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 Geta-Ve · Jul 17, 2012 at 07:28 AM · rotationdirectionaxisleanmaintain

Maintain direction regardless of rotation

Hey there! So, I am attempting to control my player on the X axis, with the left and right keys, which works thus far. I am then trying to layer on some rotational functionality, whereupon the player character 'leans' left or right dependant on movement. I have also been able to achieve this.

However, my issue arises when the player character successfully leans in to its movement but then subsequently moves in that new direction. See figure for example.

The blue square represents my player character at a standstill. While the red square represents what currently happens when it leans in to its movement. Lastly the green square represents how I would like it to act.

Blue = standstill, Red = bad axis, Green = good axis

Here is my code thus far.

 #pragma strict
 
 var playerSpeed = 10.0;
 var playerRotation = -5;
 
 function Start () {}
 
 function Update () 
 {
  var movementX = Input.GetAxis("Horizontal") * Time.deltaTime * playerSpeed; 
  var rotationZ = Input.GetAxis("Horizontal") * playerRotation;
  
  transform.Translate(movementX, 0, 0); //dictates the movementX variables direction. In this case, obviously the X axis.
  transform.localRotation = Quaternion.Euler(0,0,rotationZ);
  
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Eric5h5 · Jul 17, 2012 at 07:31 AM

Use Space.World in Translate. (Another possibility: make an empty object that's used as the transform which is controlled by the player, with the visible object as a child of the empty object. The child would be rotated but not the parent.)

Comment
Add comment · Show 5 · 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 Geta-Ve · Jul 17, 2012 at 08:27 AM 1
Share

transform.Translate(movementX, 0, 0, Space.World);

Worked perfectly! Hopefully that is how you intended me to implement it. One thing that is curious, of which I am unsure if it matters.

When I move left it shows a rotational value of 5 on the Z axis, and when I move right it shows a rotational value of 355 on the Z axis.

Thank you so much for your help!!

avatar image Geta-Ve · Jul 17, 2012 at 08:41 AM 0
Share

Upon further reflection I am guessing that it has to do with the fact that I am multiplying the Input.GetAxis by the playerRotation. Other than the values being really high, do you think this will effect performance at all?

avatar image Eric5h5 · Jul 17, 2012 at 05:12 PM 0
Share

It's just a multiplication, so it would have nearly zero effect on performance. You'd need to do millions of multiplications to get anything measurable.

avatar image Geta-Ve · Jul 17, 2012 at 07:20 PM 0
Share

Ah!! Thank you so much! :P Obviously I am entirely new to program$$anonymous$$g and Unity. You have helped immensely!

avatar image Geta-Ve · Jul 17, 2012 at 07:21 PM 0
Share

Also, if you, or anyone else is curious. Fruits of said labour.

http://youtu.be/mJ8NFJcs4Uw

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Control object yaw by two axis 1 Answer

Rotate object so a specific axis faces another object, while also relative to the parent direction 1 Answer

Make object face direction only on one axis 1 Answer

90 Degree stopping rotation on y-axis issue 0 Answers

Camera/Direction Rotation 2 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