rusty
1
Hello
Im trying to make a simple swipe to look/ pan script but am unsure how to make subsequent swipes additive to the players rotation, rather than snapping back to a central position. I have tried adding the swipe delta to the players current rotation but am unsure of the correct implementation for this. Current code below.
Thanks.
void TouchActions()
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0]; // register touches
if (touch.phase == TouchPhase.Began)
{
touchStartPosition = touch; // get position of first touch
playerStartPosition = transform.position; // get initial player position
}
else if (touch.phase == TouchPhase.Moved)
{
float deltaX = (touchStartPosition.position.x - touch.position.x);
float deltaY = touchStartPosition.position.y - touch.position.y;
transform.eulerAngles = new Vector2(0, -deltaX) * lookSpeed; // left/right
}
else if (touch.phase == TouchPhase.Ended)
{
touchStartPosition = new Touch();
}
}
}
rusty
2
Okay
I did my googles and found that I needed to get player rotation as a Quaternion and multiply by touch delta. Seems to work ok.
void TouchActions()
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0]; // register touches
if (touch.phase == TouchPhase.Began)
{
touchStartPosition = touch; // get position of first touch
playerStartPosition = transform.rotation; // get player begin rotation
}
else if (touch.phase == TouchPhase.Moved)
{
float deltaX = touchStartPosition.position.x - touch.position.x;
float deltaY = touchStartPosition.position.y - touch.position.y;
rotationY = Quaternion.Euler(0, -deltaX * lookSpeed, 0);
transform.rotation = playerStartPosition * rotationY;
}
else if (touch.phase == TouchPhase.Ended)
{
touchStartPosition = new Touch();
}
}
}