- Home /
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?
Answer by Novodantis 1 · Dec 15, 2010 at 05:02 PM
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.
I don't see any value add to how you answer compares to $$anonymous$$e.
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.
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?
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
"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.
Answer by denewbie · Dec 15, 2010 at 04:53 PM
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
this script makes my character constantly rotate the wrong direction please could you help? thanks for answering tho.
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.
Your answer
Follow this Question
Related Questions
pretty weird issue with rotating a sphere 2 Answers
Disabling the Z axis? 1 Answer
[SOLVED]Possible Alternation of Transform.Rotate 1 Answer
A node in a childnode? 1 Answer
Heya again guys how about rotation and score board 0 Answers