Question by
MofinGames · Dec 07, 2015 at 02:33 PM ·
mobilespaceshiplocalrotationtouch screen
Touch screen transform.localrotation problem
Hi!
I'm trying to rotate a spaceship using transform.localrotation and touch screen swiping motions. It works fine when the spaceship is facing forward, but when I steer downwards 90 degrees, the left/right rotation doesn't work proplerly. Here's the script attached to the spaceship:
using UnityEngine; using System.Collections;
public class Spaceship : MonoBehaviour { Vector2 initPos; float xOffset; float yOffset;
void Update()
{
foreach (Touch touch in Input.touches)
{
float x = -7.5f + 15f * touch.position.x / Screen.width;
float y = -4.5f + 9 * touch.position.y / Screen.height;
if (touch.phase == TouchPhase.Began)
{
initPos = new Vector2(x, y);
}
transform.localRotation = Quaternion.Euler(-(y - initPos.y + yOffset) * 10, (x - initPos.x + xOffset) * 10, 0);
if (touch.phase == TouchPhase.Ended)
{
yOffset += y - initPos.y;
xOffset += x - initPos.x;
}
}
}
}
Does anyone know what the issue is?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612051700im_/https://answers.unity.com/themes/thub/images/avi.jpg)