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

hugemaggot gravatar 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

Novodantis 1 gravatar 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

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

"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

hugemaggot, it means you can't call GetAxis from any other thread than the thread unity uses. This happens if you're running threads or calling asynchronous methods. It may also be as Novodantis says; you must put it in a Update function. I don't know JS well enough to know what happens in the initialization scope.

Dec 15, 2010 at 06:35 PM Statement

@Eric: Yeah, it's not so much for performance, but it means that I can use this value elsewhere in the script without passing it about [eg. a turning display in OnGUI()]. I can see what you're saying, though. If I had no plans to use it elsewhere, I'd be quite happy to limit its scope to Update(); that's technically neater.

Dec 16, 2010 at 10:25 AM Novodantis 1
(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

denewbie gravatar 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: 2376 times

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