• 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
Question by eric_c · Nov 23, 2011 at 07:50 AM · rotationjavascriptmovementcharactercontrollerlookat

Rotate character to the moving direction problems?

Hi, I made a simple character controller script here :

 var speed : float = 6.0;

var jumpSpeed : float = 8.0; var gravity : float = 20.0; var target : Transform; private var moveDirection : Vector3 = Vector3.right;

function Update() {

 var controller : CharacterController = GetComponent(CharacterController);
 // I use "target" as a world space reference, will that be a problem?
 moveDirection = Vector3.Normalize(new  Vector3(Input.GetAxis("Horizontal"), 0f, 
                                                Input.GetAxis("Vertical")));
 moveDirection *= speed;

 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;

 // Move the controller
 controller.Move(moveDirection * Time.deltaTime);
 
 transform.LookAt(target.position + moveDirection);

}

It uses the horizontal/Vertical inputs as movement, and rotates to the moving direction. It works well when you use the WASD keys to move around ( the object rotates to the moving direction ), but when you release the directional keys, the object immediately bounces back to (0,0,0) rotation... how do I make the object remain facing that moving direction even when you stopped pressing the buttons? Thanks!

Comment

People who like this

0 Show 0
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

Answer by eric_c · Nov 23, 2011 at 09:33 AM

Im stuck here and I really need some help guys...

Comment

People who like this

0 Show 0 · 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

Answer by cj_coimbra · Nov 23, 2011 at 04:36 PM

The problem is that when you release the WASD keys, your "transform.LookAt(target.position + moveDirection);" code line will read the (0,0,0), because Input.GetAxis´s calls either vertical or horizontal will always be 0 when no keys are pressed.

My quick and dirty suggestion is that you have a second variable. Always copy the moveDirection to it right after you calculate the moveDirection. Then, you apply LookAt function with moveDirection only when there are WASD keys pressed. Other than that, you apply lookAt with that secondary variable which will happen to have the last read Input.GetAxis´s values before the keys were released.

Comment

People who like this

0 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 eric_c · Nov 24, 2011 at 01:52 AM 0
Share

Another problem, I tried copying the "moveDirection" variable to another variable like you told me to, but I cant seem to copy it BEFORE the horizontal/vertical inputs are released... how do I do this?

avatar image cj_coimbra · Nov 24, 2011 at 11:27 AM 0
Share

To see if the keys are pressed, put your GetAxis code outside.

 x = Input.GetAxis("Horizontal");
 z = Input.GetAxis("Vertical");
 moveDirection = Vector3.Normalize(new  Vector3(x, 0f, z));
 
 
 if (x == 0 && z == 0)
     transform.LookAt(target.position + lastDirection);
 else 
 {
     transform.LookAt(target.position + moveDirection);
     lastDirection = moveDirection;
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I move a character to a point in a straight line with CharacterController.Move while rotating him? 2 Answers

Small Project, tutorials/help needed 0 Answers

Movement and aiming based on Arrow keys and WASD? 0 Answers

Problem using rotation and Rotate 1 Answer

Character Controller Movement - Different speeds on different axis 1 Answer


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