• 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 muzzwood · Sep 25, 2010 at 01:59 PM · movementcharactertopdownface

How to turn and face the direction I'm moving using CharacterController?

Hi all,

I'm new to Unity, and although I've got programming experience with JS these libraries sure do take a while to learn! :)

I'm working on a game with a top down view that has the camera targeted at the player character below. The camera never rotates, it keeps the same perspective.

The character needs to walk in the direction of the arrow key pressed. So there is no "forward" as such.

I had it all working nicely until my character walked through a wall and I read that I need to use Move or SimpleMove rather than Transform.position to stop this from happening.

I'm now using a slightly modified version of the script example for 'Move' in the docs.

I can move around in all directions but I can't get the player to face the direction he's walking in. Hopefully, someone can help me out :)

My script is:

var speed = 6.0; var gravity = 20.0; var bulletPrefab:Transform;

private var moveDirection = Vector3.zero;

function FixedUpdate() { var controller : CharacterController = GetComponent(CharacterController); if (controller.isGrounded) {

     moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
     Input.GetAxis("Vertical"));


 /* Here's the part I'm having trouble with... 
    If I don't add the following if block, the character moves correctly but doesn't
    face the correct direction.
    If I do add this if block, the character faces the correct direction but
    both the up and down arrow keys make the character move up the screen
    and the left and right arrows make him move down the screen. So it's like 
    I'm only accessing the vertical axis.
 */
     if (moveDirection != Vector3.zero) {
         var rotation = transform.rotation; 
         rotation.SetLookRotation(moveDirection); 
         transform.rotation = rotation;
     }


     moveDirection = transform.TransformDirection(moveDirection);
     moveDirection *= speed;



     if(Input.GetButtonDown("Jump")) {
         var bullet = Instantiate(bulletPrefab, 
                                     GameObject.Find("spawnFireBall").transform.position, 
                                     Quaternion.identity);
         bullet.rigidbody.AddForce(transform.forward * 1200);
     }
 }

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

 // Move the controller
 controller.Move(moveDirection * Time.deltaTime);

}

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
3

Answer by muzzwood · Sep 25, 2010 at 03:21 PM

Fixed.

After fiddling around with it for some time I found the answer.

I just needed to remove the following line:

moveDirection = transform.TransformDirection(moveDirection);

I was using both setLookRotation and TransformDirection and they didn't seem to like each other. TransformDirection wasn't even needed in the end :)

Hooray!

Comment
Add comment · Show 3 · 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 Will 7 · Oct 27, 2010 at 11:49 AM 0
Share

Can you mark you answer as correct please so this question comes up as answered in the list.

avatar image quaam · May 12, 2013 at 10:58 PM 0
Share

Hi can you post the correct code? I'm trying to get this to work, but I'm getting errors.

avatar image Narv · Jun 01, 2015 at 04:13 PM 0
Share

This fixed my issue, thank you! you should mark this as the answer.

avatar image
0

Answer by AliAzin · Sep 25, 2010 at 02:45 PM

You can Use "MouseLook" script in standard assets. just import standard assets from Assets>>Import packages menu.

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 muzzwood · Sep 25, 2010 at 03:00 PM 0
Share

Hi AliAzin,

Thanks for you reply! I just had a look at $$anonymous$$ouseLook but it seems to be only for the mouse. I'm unsure of how to use that script for the arrow keys on the keyboard... I think my problem is co$$anonymous$$g from

rotation.SetLookRotation(moveDirection);

but I'm not sure how to fix it.

Thanks again!

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

1 Person is following this question.

avatar image

Related Questions

Make the character face the direction he is moving? 2 Answers

Change character's Y rotation based on velocity. 1 Answer

Make a camera move towards player when the player moves out of the camera's vision 2 Answers

3D Top down character movement. 2 Answers

See through walls 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