Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by hugemaggot · Dec 15, 2010 at 04:48 PM · rotation

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image denewbie · Dec 15, 2010 at 05:04 PM 0
Share

I don't see any value add to how you answer compares to $$anonymous$$e.

avatar image Statement · Dec 15, 2010 at 05:30 PM 3
Share

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.

avatar image hugemaggot · Dec 15, 2010 at 05:47 PM 0
Share

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?

avatar image Novodantis 1 · Dec 15, 2010 at 05:59 PM 1
Share

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

avatar image Eric5h5 · Dec 15, 2010 at 06:33 PM 0
Share

"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.

Show more comments
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hugemaggot · Dec 15, 2010 at 05:43 PM 0
Share

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

avatar image denewbie · Dec 16, 2010 at 12:10 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges