I have a spaceship that I instantly want to look in the direction of a swipe event (IOS). I have tried several things but nothing works correctly. Here's the latest code I have. Any ideas? Code (csharp): #pragma strict var startpos:Vector2; var endpos:Vector2; function Update () { for(touch in Input.touches) { if(Input.GetTouch(0).phase == TouchPhase.Began){ startpos = touch.position; } if(Input.GetTouch(0).phase == TouchPhase.Ended){ endpos=touch.position; var angleTouch:float=Vector2.Angle(startpos,endpos); transform.localEulerAngles.y=angleTouch; Debug.Log(angleTouch); } //-- } //-0- }
Are you sure y is the axis? Other than that, it looks like at touch end, your ship should be rotated on y at that angle to me.
Hi ! First, Vector2.Angle will return an absolute value. if you want to get negative value, use this : Code (csharp): public static float Vector3Angle(Vector3 _from, Vector3 _to) { return Vector3.Angle(_from, _to) * ((Vector3.Cross(_from, _to).z < 0) ? -1 : 1); } Warning, this isn't necessarily z. It can be y. I don't really see what you wanted to do with this code... If you want to turn view with a finger movement, you'd better check Input.touch[0].deltaposition. If you want to turn view with a rotation finger movement, you'd better remember a Vector2 origin is the point (0,0) of the screen. You have to make it relative to the screen Center and so check difference between angles : Code (csharp): float angle = Vector3Angle(Vector2.up, startpos - new Vector2(Screen.width / 2, Screen.height / 2)) - Vector3Angle(Vector2.up, endpos - new Vector2(Screen.width / 2, Screen.height / 2)) and add it to your localEulerAngles. If you want to rotate to what your finger is pointing, you can use Transform.LookAt and Camera.ScreenPointToRay Code (csharp): Transform.lookAt(transform.position + Camera.mainCamera.ScreenPointToRay(Input.touches[0].position).direction); Hope this helps Avalion