- Home /
Snap the rotation of object
I am trying to snap the rotation of object to left and right, as i keep on clicking i dont know my there is some additional angle is being added everytime i click.
This is weird because after pressing W several time there is some additional angle is being added 104.83 is suppose to be 90, not 104.
what i am trying to do is to move object continuously in the right on x axis. and then with W, D, S keys i want them to rotate in there direction.( i am not using GetAxis because input.Getkey will be replaced by Touch after completion of game for mobile).
This is how i wont them to move
This is my script
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (Vector3.right * speed * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.W))
{
transform.Rotate (transform.rotation.x,transform.rotation.y,transform.rotation.z + 90f);
}
}
}
Answer by b1gry4n · Dec 06, 2014 at 08:50 AM
I am assuming the way you have your gameobject set up...
I notice you are rotating on the Z axis, I will do the same
0 is up
90 is right
180 is back
270 is left
if (Input.GetKeyDown (KeyCode.W)) { transform.localEulerAngles = new Vector3(0,0,0); }
if (Input.GetKeyDown (KeyCode.A)) { transform.localEulerAngles = new Vector3(0,0,270); }
if (Input.GetKeyDown (KeyCode.S)) { transform.localEulerAngles = new Vector3(0,0,180); }
if (Input.GetKeyDown (KeyCode.D)) { transform.localEulerAngles = new Vector3(0,0,90); }
By plugging in 0 for the other rotation values and not just adding 90 to our desired rotation, it will be impossible to get anything other than the desired values.
Since the rotation value is not being increasing/decreasing with increments and since the rotation value will not exceed 360, we can use eulerAngles instead of rotation. This allows us to plug in vector3 values(which I find easier to read/understand)
Hey @b1gry4n I am trying to rotate the the object but smoothly, Trying various combination of Quaternion but its not working. Which of the Lerp accepts current.rotation future.rotation and a float ?
i could not find a lerp which accepts angle
please help.
Your answer
Follow this Question
Related Questions
rotate object around another object 1:1 1 Answer
Need help with rotation script. 1 Answer
rotate around character 1 Answer
Make a Move Vector rotate around axis to run animation. 0 Answers
rotate around character 2 Answers