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 MarkusKarlsson · Oct 27, 2016 at 06:49 PM · controllerplayer movementroot motionlocomotionjetpack

Save my WEEK: Converting root motion controller script into jetpack controller script

This is driving me crazy:

I'm using this player controller that uses root motion to move and rotate the player (using single joystick). I'm struggling to convert this code into jetpack controller. Because while player is using jetpack I don't want it to have any animations. And since there's no animation player moves nowhere. If I disable root motion (locomotion system) or whatever it's called, I can use rigidbody velocity to move player forward and up but what I've been struggling for the past 4 days or so is how to change the rotation taken from animation into something that rotates player without animations. I think the problem is that when using animation to turn player will keep making this big circle instead of turning around own y-axis. I got this script by following youtube tutorial and I don't understand much about it. I just hope there's some guru that understands what I mean and can help me out.

 using UnityEngine;
 using UnityStandardAssets.CrossPlatformInput;
 using System.Collections;

 public class CharacterControllerLogic : MonoBehaviour {

 [SerializeField]
 private Animator animator;
 [SerializeField]
 private float directionDampTime = .25f;
 [SerializeField]
 private ThirdPersonCamera gamecam;
 [SerializeField]
 private float directionSpeed = 3.0f;
 [SerializeField]
 private float rotationDegreePerSecond = 120f;

 // private global only
 private float horizontal = 0.0f;
 private float vertical = 0.0f;
 private float speed = 0.0f;
 private float direction = 0.0f;
 private AnimatorStateInfo stateInfo;
 private Rigidbody rb;

 // jump
 public bool grounded = true;
 public float distToGrounded = .1f;
 public float jumpSpeed;
 public LayerMask ground;
 RaycastHit hit = new RaycastHit();
 public bool allowGrounding = false;

 // hashes
 private int m_LocomotionId = 0;

 // Use this for initialization
 void Start () {

     if (GetComponent<Animator> ()) {
         animator = GetComponent<Animator> ();
     } else {
         Debug.LogError ("Player needs an animator.");
     }

     if (animator.layerCount >= 2) {
         animator.SetLayerWeight (1, 1);
     }

     if (GetComponent<Rigidbody> ()) {
         rb = GetComponent<Rigidbody> ();
     } else {
         Debug.LogError ("Player needs to have a rigidbody.");
     }

     m_LocomotionId = Animator.StringToHash ("Base Layer.Locomotion");
 }

 // Update is called once per frame
 void Update () {
     
     horizontal = CrossPlatformInputManager.GetAxis ("Horizontal");
     vertical = CrossPlatformInputManager.GetAxis ("Vertical");

     if (grounded && CrossPlatformInputManager.GetButtonDown ("Jump") || grounded && Input.GetKeyDown (KeyCode.Space)) {
         grounded = false;
         rb.velocity = Vector3.up * jumpSpeed;
         animator.SetBool ("Jump", true);
     } else if (grounded) {
         // MOVE ON GROUND
         //rb.velocity = Vector3.up * 0;
         animator.SetBool ("Jump", false);
         stateInfo = animator.GetCurrentAnimatorStateInfo (0);

         speed = new Vector2 (horizontal, vertical).sqrMagnitude;

         // Translate controls stick coordinates into world/character space
         StickToWorldSpace (this.transform, gamecam.transform, ref direction, ref speed);

         animator.SetFloat ("Speed", speed);
         animator.SetFloat ("Direction", direction, directionDampTime, Time.deltaTime);
     } else if(!grounded) {
         // FALLING / JUMP SUCCESSFULL
         if (DistanceToGround () > distToGrounded) {
             // if we are higher up from the ground than distToGrounded
             allowGrounding = true;
         }
         if (allowGrounding) {
             // player has jumped more than value of distToGrounded from the ground
             // we can start checking when player lands
             grounded = Grounded();

             if (grounded) // allow player to jump and land again
                 allowGrounding = false;
         }
     }    
 }

 void FixedUpdate(){
     // if player is not using jetpack or car
     if (IsInLocomotion () && ((direction >= 0 && horizontal >= 0) || (direction < 0 && horizontal < 0))) {
         Vector3 rotationAmount = Vector3.Lerp (Vector3.zero, new Vector3 (0f, rotationDegreePerSecond * (horizontal < 0f ? -1f : 1f), 0f), Mathf.Abs (horizontal));
         Quaternion deltaRotation = Quaternion.Euler (rotationAmount * Time.deltaTime);
         this.transform.rotation = (this.transform.rotation * deltaRotation);
     }
 }

 public void StickToWorldSpace(Transform root, Transform camera, ref float directionOut, ref float speedOut){
     Vector3 rootDirection = root.forward;

     Vector3 stickDirection = new Vector3 (horizontal, 0, vertical);

     speedOut = stickDirection.sqrMagnitude;

     // Get camera rotation
     Vector3 CameraDirection = camera.forward;
     CameraDirection.y = .0f;
     Quaternion referentialShift = Quaternion.FromToRotation(Vector3.forward, CameraDirection);

     // Convert joystick input in Worldspace coordinates
     Vector3 moveDirection = referentialShift * stickDirection;
     Vector3 axisSign = Vector3.Cross (moveDirection, rootDirection);

     float angleRootToMove = Vector3.Angle (rootDirection, moveDirection) * (axisSign.y >= 0 ? -1f : 1f);

     angleRootToMove /= 180f;

     directionOut = angleRootToMove * directionSpeed;
 }

 public bool IsInLocomotion(){
     return stateInfo.shortNameHash == m_LocomotionId;
 }

 public bool Grounded(){
     return Physics.Raycast (transform.position, Vector3.down, distToGrounded, ground);
 } 
 public float DistanceToGround(){
     if(Physics.Raycast (transform.position, Vector3.down, out hit)) {
         return hit.distance;
     }
     return 10.0f;
 } 

}

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I disable player input action map and enable the UI action map? 4 Answers

Rigidbody Controller move forward in the direction of the camera 0 Answers

Distance travelled by a character is limited (capped) when using Mecanim 1 Answer

Player Movement 0 Answers

Rigidbody movement in direction of camera? 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