Search Unity

Help Wanted How to make the player run/walk and jump ? Blend the Jump with the Run/Walk

Discussion in 'Animation' started by JhonRA77, Sep 26, 2021.

  1. JhonRA77

    JhonRA77

    Joined:
    Sep 9, 2021
    Posts:
    51
    Now when I press the J key to jump the player stand still and jump on place.
    but I want to make that if I press W forward while the player is moving forward and than I press on J too the player will jump forward with the run/walk and not that the player will stop play the jump animation.

    This is a screenshot of my Animator Controller I added the State Jump with the animation Jump inside.
    I also have a parameter type trigger name JumpT

    Locomotion is a Blend Tree and I added two transitions from the Jump state to the Locomotion.
    the transition from Locomotion to Jump I disabled unchecked the Has Exit Time and added Conditions JumpT

    the transition from Jump to Locomotion I checked enabled true the Has Exit Time.



    And



    And the blend tree Locomotion



    And the script :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4.     public class TestMovement : MonoBehaviour
    5.     {
    6.         public bool useCharacterForward = false;
    7.         public bool lockToCameraForward = false;
    8.         public float turnSpeed = 10f;
    9.         public KeyCode sprintJoystick = KeyCode.JoystickButton2;
    10.         public KeyCode sprintKeyboard = KeyCode.Space;
    11.         public bool useJump = false;
    12.         public Vector3 jump;
    13.         public float jumpImpulse;
    14.  
    15.         private float turnSpeedMultiplier;
    16.         private float speed = 0f;
    17.         private float direction = 0f;
    18.         private bool isSprinting = false;
    19.         private Animator anim;
    20.         private Vector3 targetDirection;
    21.         private Vector2 input;
    22.         private Quaternion freeRotation;
    23.         private Camera mainCamera;
    24.         private float velocity;
    25.         private bool isGrounded;
    26.         private Rigidbody rb;
    27.  
    28.         // Use this for initialization
    29.         void Start()
    30.         {
    31.             anim = GetComponent<Animator>();
    32.             mainCamera = Camera.main;
    33.             rb = GetComponent<Rigidbody>();
    34.             jump = new Vector3(0.0f, 2.0f, 0.0f);
    35.         }
    36.  
    37.         private void OnCollisionStay(Collision collision)
    38.         {
    39.             isGrounded = true;
    40.             //anim.SetBool("Jump", false);
    41.         }
    42.  
    43.         private void Update()
    44.         {
    45.             if (Input.GetKeyDown(KeyCode.J) && isGrounded && useJump)
    46.             {
    47.                 anim.SetTrigger("JumpT");
    48.                 rb.AddForce(jump * jumpImpulse, ForceMode.Impulse);
    49.                 isGrounded = false;
    50.             }
    51.         }
    52.  
    53.         // Update is called once per frame
    54.         void FixedUpdate()
    55.         {
    56.          
    57.  
    58.             input.x = Input.GetAxis("Horizontal");
    59.             input.y = Input.GetAxis("Vertical");
    60.  
    61.             // set speed to both vertical and horizontal inputs
    62.             if (useCharacterForward)
    63.                 speed = Mathf.Abs(input.x) + input.y;
    64.             else
    65.                 speed = Mathf.Abs(input.x) + Mathf.Abs(input.y);
    66.  
    67.             speed = Mathf.Clamp(speed, 0f, 1f);
    68.             speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref velocity, 0.1f);
    69.             anim.SetFloat("Speed", speed);
    70.  
    71.             if (input.y < 0f && useCharacterForward)
    72.                 direction = input.y;
    73.             else
    74.                 direction = 0f;
    75.  
    76.             anim.SetFloat("Direction", direction);
    77.  
    78.             // set sprinting
    79.             isSprinting = ((Input.GetKey(sprintJoystick) || Input.GetKey(sprintKeyboard)) && input != Vector2.zero && direction >= 0f);
    80.             anim.SetBool("isSprinting", isSprinting);
    81.  
    82.             // Update target direction relative to the camera view (or not if the Keep Direction option is checked)
    83.             UpdateTargetDirection();
    84.             if (input != Vector2.zero && targetDirection.magnitude > 0.1f)
    85.             {
    86.                 Vector3 lookDirection = targetDirection.normalized;
    87.                 freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
    88.                 var diferenceRotation = freeRotation.eulerAngles.y - transform.eulerAngles.y;
    89.                 var eulerY = transform.eulerAngles.y;
    90.  
    91.                 if (diferenceRotation < 0 || diferenceRotation > 0) eulerY = freeRotation.eulerAngles.y;
    92.                 var euler = new Vector3(0, eulerY, 0);
    93.  
    94.                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(euler), turnSpeed * turnSpeedMultiplier * Time.deltaTime);
    95.             }
    96.         }
    97.  
    98.         public virtual void UpdateTargetDirection()
    99.         {
    100.             if (!useCharacterForward)
    101.             {
    102.                 turnSpeedMultiplier = 1f;
    103.                 var forward = mainCamera.transform.TransformDirection(Vector3.forward);
    104.                 forward.y = 0;
    105.  
    106.                 //get the right-facing direction of the referenceTransform
    107.                 var right = mainCamera.transform.TransformDirection(Vector3.right);
    108.  
    109.                 // determine the direction the player will face based on input and the referenceTransform's right and forward directions
    110.                 targetDirection = input.x * right + input.y * forward;
    111.             }
    112.             else
    113.             {
    114.                 turnSpeedMultiplier = 0.2f;
    115.                 var forward = transform.TransformDirection(Vector3.forward);
    116.                 forward.y = 0;
    117.  
    118.                 //get the right-facing direction of the referenceTransform
    119.                 var right = transform.TransformDirection(Vector3.right);
    120.                 targetDirection = input.x * right + Mathf.Abs(input.y) * forward;
    121.             }
    122.         }
    123.  }
    124.  
     
unityunity