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 agentsmith · Dec 06, 2018 at 11:19 AM · charactercontrollerdirectionfacing

How do I move a CharacterController type in the direction it's facing?

I should mention this is an NPC and needs to operate without input from the player. I'd like to drop this NPC somewhere in the game and then use the Editor's rotate tool to point him in the direction I want him to walk forward.

My Code:

 function FixedUpdate()
 {
   moveDirection = Vector3.forward;
   moveDirection *= speed;

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

   // Move the controller
   var controller : CharacterController = GetComponent(CharacterController);
   var flags = controller.Move(moveDirection * Time.deltaTime);
   grounded = (flags & CollisionFlags.CollidedBelow) != 0;
 }

When the game runs, my character moves in the Vector3.forward direction, however if I rotate him to point in a different direction (lets say 45 degrees) and start the game, then he's facing 45 degrees but is still moving in Vector.forward. I'm guessing the rotation isn't changing his Z-axis(?)...

The only thing I could think of is to take the rotation angle he's facing and figure out a way to calculate how much Z and how much X to put in his "moveDirection." For example if his rotation is 45 degrees then he's in the middle between the Z and X axis; so that's Vector3(0.5,0,0.5) 50% X-axis and 50% Z-axis. I'm guessing there's an easier way then trying to compute it manually...

I tried another solution on Unity Answers that suggested to use "transform.LookAt" but his solution didn't work for me: http://answers.unity3d.com/questions/5364/how-can-i-get-my-model-to-face-the-direction-it-is-moving

Thank you in advance!

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

Answer by spinaljack · Jul 24, 2010 at 11:24 AM

Vector3.forward is on the world axis.

You need local rotation or transform direction

e.g.

moveDirection = transform.TransformDirection(Vector3.forward)*moveSpeed;
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 Mike 3 · Jul 24, 2010 at 04:15 PM 4
Share

or transform.forward

avatar image agentsmith · Jul 24, 2010 at 05:27 PM 0
Share

Thank you spinaljack and $$anonymous$$ike; BOTH of your solutions worked. (I had to remove the "*moveSpeed"). Again thank you both very, very much!!!! @$$anonymous$$ike- I wish your answer was here so I could give you credit as well.

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

Movement based on camera 1 Answer

How can I get my character to face the direction he is moving? 1 Answer

Angle between two objects' facing direction 2 Answers

How do i shoot in the direction im facing? 3 Answers

How to make a gameobject face the same direction the joystick is pointing to? 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