Rotate object based on swipe last touch

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);
}
}
}

Please try to use code tags when you post code so it’s easier to read. There’s a pinned thread on how to use them in the forums.
Do you want to rotate based on the start and end angle?
Do you want to rotate to just where the swipe ended?

Alright, sorry.
Yes, I try to rotate to the direction where the swipe ended

My rotation “skills” are somewhat limited. I believe you can get the angle with Vector3.Angle()
You could try transform.LookAt(endPos).
Quaternion.LookRotation(endPos)
Mathf.Atan2(endPos.y - startPos.y, endPos.x - startPos.x) * Mathf.Rad2Deg. and rotate it.