Using Quaternion

I am trying to make my sprite rotate based on where the left stick is pointing. I’m trying to use Quaternions to rotate but I believe my lack of knowlege in maths is making trying to understand the documentation a bit difficult. Can anybody see what i’m doing with wrong and point me towards good resources for learning this kind of stuff.

void Update ()
	float x_pos = Input.GetAxis("Horizontal"); 
	float y_pos = Input.GetAxis("Vertical");
	rotation (x_pos,y_pos);
	movement (x_pos,y_pos);

void movement ( float x ,float y )
	if (x > 0) transform.Translate(Vector3.right * speed * Time.deltaTime);
	if (x < 0 ) transform.Translate(Vector3.left * speed * Time.deltaTime);
	if (y > 0) transform.Translate(Vector3.up * speed * Time.deltaTime);
	if (y < -0) transform.Translate(Vector3.down * speed * Time.deltaTime);

void rotation ( float x ,float y )
	float angle_rotate = Mathf.Sqrt(Mathf.Pow(x,2)+Mathf.Pow(y,2));
	Quaternion rotate = Quaternion.Euler(0, 0, angle_rotate*360);
	Debug.Log (rotate);
	transform.rotation = rotate;

Yeah, your math isn’t right.

You want a rotation around the Z axis right? based on the controller input…

float rot_z = Mathf.Atan2(y_pos, x_pos) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, rot_z);

This will set the X Axis facing the stick. If you want the Y axis to face the stick, subtract 90 from rot_z.

You might want to consider using a dead zone on the stick… and checking if x_pos and y_pos are both below the deadzone threshold, and not rotate if they are.

Otherwise they’ll rotate to face the right instead of continuing in the same direction if they take their hand off the stick.