David Moreno
David Moreno

Reputation: 161

How to ignore rotation while moving? (In Unity using C#)

I'm very noob at programming but I wanted to try something using some tutorials.

I basically have an object and using Android's accelerometer it rotates, then by using two buttons, it either goes left or right. For this I use

transform.Rotate (0, 0, -temp * Time.deltaTime);

and

transform.Translate (speed * Time.deltaTime, 0, 0);

The program works properly and once the object has rotated, it moves in the "new x axis", but I want it to move in the "world's x axis", one that's "permanent".

As I'm very bad at programming and English as well I made a simple drawing so it's easier to understand what do I want to do:

https://i.sstatic.net/LpM1J.png

Being the upper case what happens (and should happen as it has been programmed) and the other what I want to achieve. Any suggestions on how to do this and "ignore" the rotation of the object?

Thanks for your time :D

Upvotes: 3

Views: 1592

Answers (1)

Maxim Kamalov
Maxim Kamalov

Reputation: 765

As described in the documentation, Transform.Rotate() indeed rotates in local space by default, but can be configured to use world space:

transform.Rotate (0, 0, -temp * Time.deltaTime, Space.World);

Same with Translate():

transform.Translate (speed * Time.deltaTime, 0, 0, Space.World);

Upvotes: 2

Related Questions