Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Thijsx7 · Dec 11, 2016 at 08:53 PM · c#animationscharacter movementroot motionturning

Root motion makes my character unable to turn.

I am currently working on a third person game that uses root motion to move the character. My problem is that the character does not turn around anymore and just moves forward in a certain direction. It should face forward relative to the camera when I press the forward key, and it should turn right relative to the camera when I press the "d" key, but it just moves forward. I am using a model imported from a Blender project file.

Here is my character movement script, note that the speed variables are set to 0 in the editor so the character only moves using it's animations. The reason I add some force to the rigidbody is because it fixed a bug where the model began vibrating after walking into corners.

 using UnityEngine;
 using System.Collections;
 
 public class CharacterMovement : MonoBehaviour {
 
     public float runSpeed = 0.5f;
     public float sprintSpeed = 1.0f;
 
     public float turnSmoothing = 3.0f;
     public float speedDampTime = 0.1f;
 
     public float speed;
 
     private Vector3 lastDirection;
 
     private Animator anim;
     private int speedFloat;
   
 
    
     private int groundedBool;
     private Transform cameraTransform;
 
     private float h;
     private float v;
 
     private bool run;
     private bool sprint;
 
     private bool isMoving;
     
     
     private float distToGround;
     Rigidbody rb;
     
 
     void Awake()
     {
         anim = GetComponent<Animator>();
         cameraTransform = Camera.main.transform;
 
         speedFloat = Animator.StringToHash("Speed");
         // fly
         groundedBool = Animator.StringToHash("Grounded");
         //distToGround = GetComponent<Collider>().bounds.extents.y;
         rb = GetComponent<Rigidbody>();
 
         
     }
 
     bool IsGrounded()
     {
         return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
     }
 
     void Update()
     {
         h = Input.GetAxis("Horizontal");
         v = Input.GetAxis("Vertical");
         run = Input.GetButton("Run");
         sprint = Input.GetButton("Sprint");
         isMoving = Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1;
         MovementManagement(h, v, run, sprint);
         Moving();
     }
 
     void FixedUpdate()
     {
         //Rigidbody rb = GetComponent<Rigidbody>();
         //rb.MovePosition(transform.position + transform.forward * speed);
         //rb.velocity = Vector3.forward * speed;
         
 
     }
 
     void MovementManagement(float horizontal, float vertical, bool running, bool sprinting)
     {
         Rotating(horizontal, vertical);
         
 
         if (isMoving)
         {
             if (sprinting)
             {
                 speed = sprintSpeed;
                 anim.SetBool("isSprinting", true);
             }
             else
             {
                 speed = runSpeed;
                 anim.SetBool("isRunning", true);
                 anim.SetBool("isSprinting", false);
 
             }
 
             
         }
         else
         {
             speed = 0f;
             anim.SetBool("isRunning", false);
             anim.SetBool("isSprinting", false);
         }
         
     }
     // Abracababra begins
     Vector3 Rotating(float horizontal, float vertical)
     {
         Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
         
         forward.y = 0.0f;
         forward = forward.normalized;
 
         Vector3 right = new Vector3(forward.z, 0, -forward.x);
 
         Vector3 targetDirection;
 
         float finalTurnSmoothing;
         
         targetDirection = forward * vertical + right * horizontal;
         finalTurnSmoothing = turnSmoothing; 
 
         if ((isMoving && targetDirection != Vector3.zero))
         {
             Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
             Quaternion newRotation = Quaternion.Slerp(GetComponent<Rigidbody>().rotation, targetRotation, finalTurnSmoothing * Time.deltaTime);
             GetComponent<Rigidbody>().MoveRotation(newRotation);
             lastDirection = targetDirection;
         }
 
         if (!(Mathf.Abs(h) > 0.9 || Mathf.Abs(v) > 0.9))
         {
             Repositioning();
             
         }
 
         return targetDirection;
     }
 
     private void Repositioning()
     {
         Vector3 repositioning = lastDirection;
         if (repositioning != Vector3.zero)
         {
             repositioning.y = 0;
             Quaternion targetRotation = Quaternion.LookRotation(repositioning, Vector3.up);
             Quaternion newRotation = Quaternion.Slerp(GetComponent<Rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);
             GetComponent<Rigidbody>().MoveRotation(newRotation);
         }
     }
     // Abracadabra ends
 
     private void Moving()
     {
         //transform.position += transform.forward * Time.deltaTime * speed;
         //Vector3 movement = new Vector3(h, 0.0f, v);
         //GetComponent<Rigidbody>().velocity = transform.forward * speed;
         transform.position += transform.forward * Time.deltaTime * speed;
         rb.AddForce(transform.forward);
     }
 
 }

I hope I gave enough information, please tell me if I didn't. Thanks for helping.

Greetings,

Thijs

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

Answer by Thijsx7 · Dec 17, 2016 at 03:34 PM

I solved the problem myself. It seemed that the root motion blocked the rotation of the rigidbody. I don't understand why but changing:

 GetComponent<Rigidbody>().MoveRotation(newRotation);

To:

 transform.rotation = newRotation;

worked. My character is now able to turn again. I am just leaving this here so people who have the same problem as me might be able to fix it too.

Greetings,

Thijs

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

252 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 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 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 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 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 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 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Character and Camera motion smooth in Editor, but jitters in Build 1 Answer

witch approach for an car race curve 1 Answer

Character movement from A to B to C in coroutine 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