Preface: I have searched a bunch and read many answers to questions and threads. None have really helped me complete this properly. I wouldn’t be surprised if its just me being the problem. It is making me feel pretty dumb.
Goal: Using single finger touch, I am trying to make an additive rotation for a game object only that only rotates on the z-axis, based on the difference between the previous touch position and the current touch position.
public void RotateShape()
{
if(Input.touches[0].phase == TouchPhase.Moved)
{
Vector3 go_Pos = myCam.WorldToScreenPoint(transform.position);
Vector2 go_ScreenPos = new Vector2(go_Pos.x, go_Pos.y);
Vector2 dPos = Input.touches[0].deltaPosition;
float newAngle = Mathf.Atan2(dPos.y - go_ScreenPos.y, dPos.x - go_ScreenPos.x);
transform.Rotate(0, 0, newAngle);
}
}
(I know I could just make a variable for Input.touches[0], use less variables, etc. I am trying to make it easy to follow)
This particular code block only rotates clockwise. It also doesn’t care about the deltaPosition difference - it pretty much always rotates at the same speed.
I have been stuck on this problem for a while and I haven’t been able to solve it correctly. I have ran into these issues: either the direction only rotates counter-clockwise or clock-wise (never both), jittery (probably just need a threshold), or reverses direction when the mid-point touch is reached. There have been a lot of iterations but to no avail.
I don’t necessarily want someone to write the code for me, I just need someone to help walk me through the proper way to do this as it is a bit frustrating.