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 Newbie100 · Oct 21, 2017 at 03:01 PM · animator controlleranimationsthird person controllerwalkcycle

Having trouble inserting character into existing third person character controller - How can I script it so that I can move in directions?

Im trying to put my modeled character into the 3rd person controller + Fly. character controller.

My model has stationary looping animations for walk and run, as in only the motion is animated but not the movement in world space itself so when I replaced the movement animations in the controller, my model does not move. I then tried to find out how to fix this and found the scripting root motion page and it works in that I can now move my character but it only moves in a straight line no matter what direction I input.

If anyone can help me find out how to script the movement so that I can move in all directions not just straight I would greatly appreciate it, because I have tried and couldn't make it work.

This is the relevant parts of the 3rd person movement script.

 // Deal with the basic player movement
 void MovementManagement(float horizontal, float vertical)
     {
         // On ground, obey gravity.
         if (behaviourManager.IsGrounded())
             behaviourManager.GetRigidBody.useGravity = true;
 
         // Call function that deals with player orientation.
         Rotating(horizontal, vertical);
 
         // Set proper speed.
         Vector2 dir = new Vector2(horizontal, vertical);
         speed = Vector2.ClampMagnitude(dir, 1f).magnitude;
         // This is for PC only, gamepads control speed via analog stick.
         speedSeeker += Input.GetAxis("Mouse ScrollWheel");
         speedSeeker = Mathf.Clamp(speedSeeker, walkSpeed, runSpeed);
         speed *= speedSeeker;
         if (behaviourManager.isSprinting())
         {
             speed = sprintSpeed;
         }
 
         behaviourManager.GetAnim.SetFloat(speedFloat, speed, speedDampTime, Time.deltaTime);
     }
 
     // Rotate the player to match correct orientation, according to camera and key pressed.
     Vector3 Rotating(float horizontal, float vertical)
     {
         // Get camera forward direction, without vertical component.
         Vector3 forward = behaviourManager.playerCamera.TransformDirection(Vector3.forward);
 
         // Player is moving on ground, Y component of camera facing is not relevant.
         forward.y = 0.0f;
         forward = forward.normalized;
 
         // Calculate target direction based on camera forward and direction key.
         Vector3 right = new Vector3(forward.z, 0, -forward.x);
         Vector3 targetDirection;
         targetDirection = forward * vertical + right * horizontal;
 
         // Lerp current direction to calculated target direction.
         if((behaviourManager.IsMoving() && targetDirection != Vector3.zero))
         {
             Quaternion targetRotation = Quaternion.LookRotation (targetDirection);
 
             Quaternion newRotation = Quaternion.Slerp(behaviourManager.GetRigidBody.rotation, targetRotation, behaviourManager.turnSmoothing);
             behaviourManager.GetRigidBody.MoveRotation (newRotation);
             behaviourManager.SetLastDirection(targetDirection);
         }
         // If idle, Ignore current camera facing and consider last moving direction.
         if(!(Mathf.Abs(horizontal) > 0.9 || Mathf.Abs(vertical) > 0.9))
         {
             behaviourManager.Repositioning();
         }
 
         return targetDirection;
     }


and this is the root motion script that I inserted in

  void OnAnimatorMove()
     {
             Animator animator = GetComponent<Animator>(); 
                               
             if (animator)
             {
      Vector3 newPosition = transform.position;
                newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime; 
      transform.position = newPosition;
             }
     }

Both of those in conjunction resulted in my character only being able to move forward in a straight line no matter if I pressed forward, back, left, or right. Thanks 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
0

Answer by IronKnigh · Aug 20, 2018 at 04:02 AM

Hello there, I recommend you check out my post which should answer your question. There are two parts to my answer and the first part should apply to your question.

https://answers.unity.com/questions/1506873/how-to-animate-player-rotation-with-respect-to-cam.html?childToView=1544087#answer-1544087

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

71 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

Related Questions

How to rotate an object with a script after an animation has rotated it? 1 Answer

Unable to play animation continuously? 1 Answer

loop an animation if the button witch activates it is still pressed 0 Answers

going back to the default state in animator 1 Answer

The 'correct' way to deal with animations in a grid-based game? 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