- Home /
Rotate an object around z axis on user input
I'm trying to rotate a 2D object around its z-axis when the user presses left or right. For some reason, my object only turns partway before it sticks and goes no further (the angle it sticks at depends on the speed).
It also turns back to zero if I let go of the key, which I don't want.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour {
public int speed;
void Start () {
speed = 100;
}
void Update () {
//Check to see if there's input
if (Mathf.Abs(Input.GetAxis ("Horizontal")) > 0) {
//Set a rate at which we should turn
float turnSpeed = speed * Time.deltaTime;
//Connect turning rate to horizonal motion for smooth transition
float rotate = Input.GetAxis ("Horizontal") * turnSpeed;
//Get current rotation
float currentRotation = gameObject.transform.rotation.z;
//Add current rotation to rotation rate to get new rotation
Quaternion rotation = Quaternion.Euler (0, 0, currentRotation + rotate);
//Move object to new rotation
gameObject.transform.rotation = rotation;
}
}
}
Answer by FabDynamic · Nov 15, 2017 at 03:21 AM
Update: Hey I figured out an even smaller modification to your code! I suspect you knew that your code was close and boy were you right! 12 characters away from perfect to be precise!
Change this line from:
float currentRotation = gameObject.transform.rotation.z;
To:
float currentRotation = gameObject.transform.rotation.eulerAngles.z;
Rationale: Apparently there is a z value of the rotation but this is not the z you are looking for ;). Its apparently the Z value of the quaternion directly and those are way above my pay grade (so to speak) so I'm not even going to try to explain what that means but I know that modifying those directly lets us easily shoot ourselves in the foot.
That should do the trick (it did for me!)
Original Answer: Try this small modification. I just commented out a few lines and added one new line which uses the built in function Transform.Rotate() which I think is just what you are looking for. It also uses the pre-defined Vector3.Forward which in this context represents the Z axis for us.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ARARotatingAroundZ : MonoBehaviour {
public int speed = 100;
void Update () {
//Check to see if there's input
if (Mathf.Abs (Input.GetAxis ("Horizontal")) > 0) {
//Set a rate at which we should turn
float turnSpeed = speed * Time.deltaTime;
//Connect turning rate to horizonal motion for smooth transition
float rotate = Input.GetAxis ("Horizontal") * turnSpeed;
// //Get current rotation
// float currentRotation = gameObject.transform.rotation.z;
// //Add current rotation to rotation rate to get new rotation
// Quaternion rotation = Quaternion.Euler (0, 0, currentRotation + rotate);
// //Move object to new rotation
// gameObject.transform.rotation = rotation;
gameObject.transform.Rotate (Vector3.forward * rotate);
}
}
The final line rotates around the z axis by the magnitude that your code already correctly calculated.