- Home /
Rotating an object on Z axis while moving
Hello, I really don't know what should I do.. I have no more ideas :/ I've already seen some q&a about rotating but "I still haven't found what I'm looking for" :D I'm creating 2D game and now, I'm making object rotations. I think my English is bad so I add a picture for you with that, what I'm thinking about.
Object has to rotate only on Z axis while its moving up or down or sides.
I wrote that, but an object is just making a really silly things when I move - I know that :D .
public float speed;
void Update () {
float movelr = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
transform.Translate (movelr, 0, 0);
float moveud = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.Translate (0, moveud, 0);
if(Input.GetKey(KeyCode.C))
{
transform.Rotate(0,0, Time.deltaTime * 180);
}
//and I have gravity, when object is falling I have to rotate him on Z axis, I hope you understand me :/
if (transform.position.y > -4.5f)
{
Physics.gravity = new Vector3 (0, -4.5f, 0);
}
}
I start with unity and C#, please guys, help me :) Thanks
Answer by zharik86 · Dec 27, 2013 at 09:46 AM
The Rotate function by you is selected absolutely correctly. But it has some distinctions in case of parameter passing. That your object rotated only round an axis z (as far as I understood, it is its coordinate axes are taken as a basis), simply write so:
transform.Rotate(Vector3.forward, Time.deltaTime * 180, Space.self)
Here, Vector3.forward says that you turn on an axis z, Space.self - use of coordinate axes of turned object. If it is necessary to you that object was turned round a world axis z, simply replace Space.self with Space.world.
Your answer
Follow this Question
Related Questions
Function to rotate my Player smoothly taking the y and use it to have angle 1 Answer
How to rotate object slowly on only Z axis 2 Answers
Is there a better way to do rotations with new Unity 2D? 1 Answer
how to know if an axis has been full rotated? 1 Answer
Player Arm will not rotate when moving with Xbox Joystick 1 Answer