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
3
Question by J-F · May 15, 2015 at 06:33 PM · rotationmovementcharacter controller

Rotate a character controller in the direction of input axis.

Im using the character controller.move example http://docs.unity3d.com/ScriptReference/CharacterController.Move.html

And im trying to rotate the player to the direction the character is moving with:

 transform.LookAt(transform.position + movementVector);

However, this just makes the player rotate whenever any input is given on the horizontal axis, this causes the player to just keep turning whenever it moves sideways.

Preset directions wont work for me because im using joystick input, So diagonal directions have to work too.

Comment
Add comment · Show 2
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 maccabbe · May 15, 2015 at 06:40 PM 0
Share

In your game do you use +y as "up"?

avatar image J-F · May 15, 2015 at 06:46 PM 0
Share

I've tried that too. $$anonymous$$akes no difference.

3 Replies

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

Answer by $$anonymous$$ · May 15, 2015 at 07:09 PM

I've tryied something out, see if this is what you're looking for: (It ignores Y axis as movement)

 private CharacterController CharCtrl;
 
     private void Start()
     {
         CharCtrl = this.GetComponent<CharacterController>();
     }
 
     private void Update()
 {
     Vector3 NextDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

     if (NextDir != Vector3.zero)
         transform.rotation = Quaternion.LookRotation(NextDir);

     CharCtrl.Move(NextDir / 8);
 }
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 J-F · May 15, 2015 at 07:28 PM 0
Share

Works perfectly, Thanks!

avatar image RickshawDerpyDerp · Jul 10, 2019 at 02:05 AM 0
Share

This works when directional input is being received but not when there is no directional input. How can we make Unity keep our character facing the direction of the previous input?

avatar image mayankela21 · Jun 06, 2020 at 09:21 AM 0
Share

Good! But When Spawning Character Player $$anonymous$$oveing Up to the Surface(Ground) this is Not Well to Cover Problem

avatar image
0

Answer by rjbresett · May 11, 2017 at 02:05 PM

Know this is an old thread but I was having a similar issue. While the character would rotate properly, it was only moving to the right. Note that I am using the reference script from the CharacterColtroller.Move API

The fix ended up being to multiply the move speed by -1 (if it is a positive number) if you were facing left and want to move left. You also need to make sure that it is returned to a positive number once you want to rotate and move to the right.

Comment
Add comment · 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
0

Answer by mayankela21 · Jun 07, 2020 at 10:42 AM

I find A Solution And Donot Ignore y Axis And Gontain Gravity, Working when Spawning thats Work:

 public Joystick joystek;

 public float movement_Speed;

 CharacterController charCtrl;

 public static float xMove, zMove;

 //private Vector3 facDir;

 void Awake()
 {
     charCtrl = GetComponent<CharacterController>();
 }

 void Update()
 {
     playerMovmentControler();
     playerRotationControler();
 }

 // Update is called once per frame
 void playerMovmentControler()
 {
     xMove = Input.GetAxis("Horizontal");// + joystek.Horizontal;
     zMove = Input.GetAxis("Vertical");// + joystek.Vertical;

     float gravity = 9.8f;
     Vector3 moveAxis = new Vector3(xMove, -gravity, zMove);
     charCtrl.Move(((moveAxis) * movement_Speed * Time.deltaTime));
 }

 void playerRotationControler()
 {
     if (xMove > 0)
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 2f * Time.deltaTime);
     else if (xMove < 0)
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 2f * Time.deltaTime);

     if (zMove > 0)
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 2f * Time.deltaTime);
     else if (zMove < 0)
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 2f * Time.deltaTime);
 }

When you Spawning Charactercontroller upper to the Surface Use this to proper rotation with Gravity

Comment
Add comment · 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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

8 directional movement rotation problem 0 Answers

Line not working in C#? 1 Answer

made a ring collider from sphere colliders and a bar collider, but when animated the ring doesn't stay on bar 0 Answers

Help regarding "Collect Cubes" Like Player Movement and Rotation 1 Answer

Rigidbody.MovePosition doesn't move reliably? 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