• 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 Grant 2 · Aug 05, 2010 at 05:48 AM · movementaimsteering

facing direction that does not return to zero

I am new to Unity but still a very experienced Python coder and Maya Animator so forgive me if I ask stupid questions in UnityScript. :)

I am trying to develop a controller for moving the character around. Think of the set-up as controlling a radio controlled car and watching it from above, looking strait down at it. In the game the player moves the character along the ground along x and z axis. The camera, for now, is simply moved up 20 units and looking strait down at the character.

I can move the character around with a simple control script like the following:

var speed = 10;

function Update () { //print("UPDATING");

 var horizontalInput = Input.GetAxis("Horizontal");
 var verticalInput = Input.GetAxis("Vertical");

 var xPos = horizontalInput * Time.deltaTime * speed;
 var zPos = verticalInput * Time.deltaTime * speed;

 transform.Translate( xPos, 0, zPos );

}

What I can't figure out is how do I steer the character in the direction of the motion? I've seen a couple of answers for something similar that I have tried but they all have one undesirable result, they return the character back to facing at it's origin (0 degrees north) when the motion stops. The problem I am running into is when the player stops, the car shouldn't magically return to facing up the Z-Axis, it should stay facing in the last direction it was moving in.

Also, I am applying the script to a generic GameObject with the geometry for the car parented underneath it. Should I be using a CharacterController instead?

I hope this all makes sense and any help you can spare would be greatly appreciated.

Thanks, Grant

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
0
Best Answer

Answer by Eric5h5 · Aug 05, 2010 at 06:59 AM

var speed = 10.0;

function Update () { var xPos = Input.GetAxis("Horizontal") Time.deltaTime speed; var zPos = Input.GetAxis("Vertical") Time.deltaTime speed;

 transform.position.x += xPos;
 transform.position.z += zPos;

 var move = Vector3(xPos, 0.0, zPos);
 if (move != Vector3.zero) {
     var rotation = transform.rotation; 
     rotation.SetLookRotation(move); 
     transform.rotation = rotation;
 }

}

You're better off using "var speed = 10.0" or "var speed : float = 10", because otherwise speed is limited to integers (it's also a teensy bit slower, since the int has to be converted to a float every time it's used).

I changed the movement so it's setting the transform.position directly instead of using Translate. Since Translate is relative, the movement wouldn't work the same anymore when you change the heading.

Comment
Add comment · Show 1 · 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 Grant 2 · Aug 06, 2010 at 10:17 PM 0
Share

Thanks Eric. I'll give it a try and see how it works out. Thanks for the tip on the whole float/int conversion, it's good to know those little details especially with performance.

To understand the code without trying it, in this case you just capture the rotation of the object's tranform, use the "SetLookRotation" method to explicitly set the direction to the heading of the vector and then re-apply it back to the transform's rotation. Seems simple enough.

I think all the solutions using "lookAt" methods was tripping me up.

Thanks again, Grant

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Animating 2D sprite upon movement script help. 2 Answers

RTS Movement - I'm doing it wrong 2 Answers

3rd Person Gun Aim 1 Answer

Why does my input overrides my player controller script. 0 Answers

"sneak mode" (ctrl key) stops my movement. 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