Hi everyone,
I’m currently having difficulties rotating my object based on the last swipe touch position in my touchEnd condition.
I am able to get the first touch position, the last touch position and the distance between them.
Some help would be greatly apreciated,
thanks.
private Vector3 playerPos;
private Vector2 touchPosFrom;
private Vector2 touchPosTo;
private float dist;
private Touch touchScreen;
public float speedRotation = 5;
private Vector3 dir;
private Quaternion lookRotation;
private bool touchEnd = false;
private Vector3 newTouchPosTo;
private Quaternion rotation;
void Update()
{
if (Input.touchCount == 1)
{
touchScreen = Input.GetTouch(0);
switch (touchScreen.phase)
{
case TouchPhase.Began:
playerPos = transform.position;
touchPosFrom = touchScreen.position;
touchEnd = false;
break;
case TouchPhase.Ended:
touchPosTo = touchScreen.position;
dist = Vector3.Distance(touchPosFrom, touchPosTo);
touchEnd = true;
break;
}
}
if (touchEnd)
{
//newTouchPosTo = new Vector3(touchPosTo.x, 0, touchPosTo.y); //touchPosTo.x, 0, touchPosTo.y
//dir = (newTouchPosTo - transform.position).normalized;
rotation = Quaternion.Euler(new Vector3(0, touchPosTo.x, 0));
//lookRotation = Quaternion.LookRotation(new Vector3(touchPosTo.x, 0, 0));
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speedRotation);
}
}
}