TouchScript / TouchScript

Complete multi-touch solution for Unity: iOS, Android, Windows, TUIO.
Other
1.67k stars 364 forks source link

Transformer2D issue with PanGesture when object position gets changed in another script #118

Closed marked-one closed 10 years ago

marked-one commented 10 years ago

In Transformer2D script it is better to replace

        private void Update()
        {
            var fraction = Speed*Time.deltaTime;
            if (transform.localPosition != lastLocalPosition)
            {
                // changed by someone else
                localPositionToGo = transform.localPosition;
            }

with

    private void Update ()
    {
        var fraction = Speed * Time.deltaTime;

        // changed by someone else
        // 9.99999944E-11f value is taken from operator != implementation of Vector3
        if (Math.Abs (transform.localPosition.x - lastLocalPosition.x) > 9.99999944E-11f) {
            localPositionToGo.x = transform.localPosition.x;
        }
        if (Math.Abs (transform.localPosition.y - lastLocalPosition.y) > 9.99999944E-11f) {
            localPositionToGo.y = transform.localPosition.y;
        }
        if (Math.Abs (transform.localPosition.z - lastLocalPosition.z) > 9.99999944E-11f) {
            localPositionToGo = transform.localPosition;
        }

(or with better code, just change coordinates separately), if there are no strong reasons to keep it as it is.

The above changes solve the following problem for me:

I was using TouchScript to move a scene in a 2d game. I added PanGesture and Transformer2D script to the root object of a scene, and everything worked fine until I decided to limit the scene movement to the camera bounds. When the object goes beyond bounds,I just default its position to the max (or min) allowed value in LateUpdate of another script.

And the problem is, when the object hits, for example, a bound in X-axis, and I then continue to move the object with a gesture in the same direction in X axis, and in whatever direction in Y axis simultaneously, it doesn't move in X axis (this is expected), but it also doesnt't move, or just moves to a small distance, in the Y axis (and normal movement is expected here).

valyard commented 10 years ago

Is it localPositionToGo.z = transform.localPosition.z; in the last if? Maybe I just woke up but I don't see how this code helps o.O

marked-one commented 10 years ago

Yes, that's my fault, localPositionToGo.z = transform.localPosition.z; is exactly what should be there. I just sent you a scene, which demonstrates what I'm talking about, to v@lent.in