Rotation script

can anyone please write a script that will allow my character to rotate when either a or d is pressed ive tried tons of other scripts but none of them work please can anyone help?

more ▼

asked Dec 15, 2010 at 04:48 PM

avatar image

183 110 87 90

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

By default, the A and D keys (along with <- & -> ) are mapped to the Horizontal Axis input.

So you can also use:

var degs : float = 30; // degrees per second var rot :float; // we'll take these out so we aren't defining every frame

function Update(){ rot = Input.GetAxis("Horizontal"); // returns a value between -1 and +1 transform.Rotate(0, rot degs Time.deltaTime, 0); }

This should rotate the object in the direction of your horizontal control (left and right or A and D) on the Y axis, by degs degrees per second.

more ▼

answered Dec 15, 2010 at 05:02 PM

avatar image

Novodantis 1
1.8k 28 34 56

I don't see any value add to how you answer compares to mine.

Dec 15, 2010 at 05:04 PM denewbie

I think Novodantis understood the OP wanted rotation in different directions depending on which key is pressed, and it uses the Input system so the input method isn't hard coded. Don't downvote people because they give alternative answers. This isn't a race for reputition points. This is a race to better everyones understanding about Unity. I think this is a nice alternative to your answer.

Dec 15, 2010 at 05:30 PM Statement

when i add the script you have given me nothing happens and the console says: getaxis can only be called from the main thread

what does this mean and how can i fix it?

Dec 15, 2010 at 05:47 PM hugemaggot

Yeah, it is just an alternative. As for why it cannot be called, hugemaggot, you need to put that in an Update() loop. I will edit the post to illustrate this better

Dec 15, 2010 at 05:59 PM Novodantis 1

"rot" shouldn't really be defined outside Update. Variables should usually be local where possible for structural reasons, and defining it outside Update doesn't improve performance.

Dec 15, 2010 at 06:33 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

There ya go...

var target:Transform; var isRotate = false; var rotationSpeed:float = 100;

function Update(){

if ( Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) ) isRotate = true;

if (isRotate) target.Rotate(Vector3.forward Time.deltaTime rotationSpeed ); }

Please double check for bugz cause I havent the chance to check it yet

more ▼

answered Dec 15, 2010 at 04:53 PM

avatar image

757 10 8 28

this script makes my character constantly rotate the wrong direction please could you help? thanks for answering tho.

Dec 15, 2010 at 05:43 PM hugemaggot

when you look at the line : target.Rotate(Vector3.forward Time.deltaTime rotationSpeed ); You can replace the "Vector3.forward" with "Vector3.left", "Vector3.right", "Vector3.up" etc to get your desired direction.

Dec 16, 2010 at 12:10 AM denewbie
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 15, 2010 at 04:48 PM

Seen: 2404 times

Last Updated: Dec 15, 2010 at 04:48 PM