Search Unity

How can I active jump only after activated jump animation ?

Discussion in 'Getting Started' started by Lupinder, Jul 4, 2020.

  1. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    I want to activate jump script only when my animation started. I dont wanna use jump unless my animation is not active. How can ı do that ? Thanks for help.

    Code (CSharp):
    1. using System;
    2. using ProInput.Scripts;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. namespace Player {
    7.  
    8.  
    9.  
    10.     public class PlayerController : MonoBehaviour {
    11.  
    12.         Animator animator;
    13.         public bool Run = false;
    14.  
    15.         void Awake()
    16.         {
    17.  
    18.             animator = GetComponent<Animator>();
    19.         }
    20.  
    21.  
    22.         [Header("Movement")]
    23.         public float movementSpeed;
    24.         public float Accelaration;
    25.         public float airMovementSpeed;
    26.         public float maxSpeed;
    27.    
    28.      
    29.         [Header("Friction")]
    30.         public float friction;
    31.         public float airFriction;
    32.  
    33.         [Header("Rotation")]
    34.         public float rotationSensitivity;
    35.         public float rotationBounds;
    36.  
    37.         [Header("Gravity")]
    38.         public float extraGravity;
    39.  
    40.         [Header("Ground Detection")]
    41.         public LayerMask whatIsGround;
    42.         public float checkYOffset;
    43.         public float checkRadius;
    44.         public float groundTimer;
    45.  
    46.         [Header("Jumping")]
    47.         public float jumpForce;
    48.         public float jumpCooldown;
    49.  
    50.         [Header("Data")]
    51.         public Camera playerCamera;
    52.         public Transform playerCameraHolder;
    53.         public Rigidbody playerRigidBody;
    54.  
    55.         private InputObject _forwardsInput;
    56.         private InputObject _backwardsInput;
    57.         private InputObject _leftInput;
    58.         private InputObject _rightInput;
    59.         private InputObject _jumpInput;
    60.         private InputObject _runInput;
    61.  
    62.         private float _xRotation;
    63.         private float _yRotation;
    64.         private float _grounded;
    65.         private bool _realGrounded;
    66.         private float _jumpCooldown;
    67.      
    68.         private void Start() {
    69.             _forwardsInput = new InputObject("Forwards", Key.W);
    70.             _backwardsInput = new InputObject("Backwards", Key.S);
    71.  
    72.  
    73.             _runInput = new InputObject("run", Key.LeftShift  );
    74.  
    75.             _leftInput = new InputObject("Left", Key.A);
    76.             _rightInput = new InputObject("Right", Key.D);
    77.             _jumpInput = new InputObject("Jump", Key.Space);
    78.         }
    79.  
    80.         private void FixedUpdate() {
    81.             GroundCheck();
    82.             ApplyMovement();
    83.             ApplyFriction();
    84.             ApplyGravity();
    85.             RunMovement();
    86.             Movement();
    87.         }
    88.  
    89.         private void Update() {
    90.             Rotation();
    91.             Jumping();
    92.         }
    93.  
    94.         private void GroundCheck() {
    95.             _grounded -= Time.fixedDeltaTime;
    96.             var colliderList = new Collider[100];
    97.             var size = Physics.OverlapSphereNonAlloc(transform.position + new Vector3(0, checkYOffset, 0), checkRadius, colliderList, whatIsGround);
    98.             _realGrounded = size > 0;
    99.             if (_realGrounded)
    100.                 _grounded = groundTimer;
    101.          
    102.            
    103.          
    104.         }
    105.         private void Movement()
    106.         {
    107.             if (_realGrounded==false)
    108.  
    109.             {
    110.                 var axis = new Vector2(
    111.                (_leftInput.IsPressed ? -1 : 0) + (_rightInput.IsPressed ? 1 : 0),
    112.  
    113.  
    114.  
    115.                (_backwardsInput.IsPressed ? -1 : 0) + (_forwardsInput.IsPressed ? 1 : 0)
    116.            ).normalized;
    117.  
    118.                 var speed = _realGrounded ? movementSpeed : airMovementSpeed;
    119.  
    120.                 var vertical = axis.y * speed * Time.fixedDeltaTime * transform.forward;
    121.  
    122.  
    123.                 var horizontal = axis.x * speed * Time.fixedDeltaTime * transform.right;
    124.  
    125.                 if (CanApplyForce(vertical, axis))
    126.                     playerRigidBody.velocity += vertical;
    127.  
    128.                 if (CanApplyForce(horizontal, axis))
    129.                     playerRigidBody.velocity += horizontal;
    130.  
    131.             }
    132.         }
    133.         private void ApplyMovement() {
    134.  
    135.  
    136.  
    137.             { }
    138.             var axis = new Vector2(
    139.                 (_leftInput.IsPressed ? 0 : 0) + (_rightInput.IsPressed ? 0 : 0),
    140.            
    141.  
    142.  
    143.                 (_backwardsInput.IsPressed ? -1 : 0) + (_forwardsInput.IsPressed ? 1 : 0)
    144.             ).normalized;
    145.  
    146.  
    147.  
    148.  
    149.  
    150.             var speed = _realGrounded ? movementSpeed : airMovementSpeed;
    151.        
    152.             var vertical = axis.y * speed * Time.fixedDeltaTime * transform.forward;
    153.  
    154.        
    155.             var horizontal = axis.x * speed * Time.fixedDeltaTime * transform.right;
    156.  
    157.             if (CanApplyForce(vertical, axis))
    158.                 playerRigidBody.velocity += vertical;
    159.          
    160.             if (CanApplyForce(horizontal, axis))
    161.                 playerRigidBody.velocity += horizontal;
    162.         }
    163.  
    164.         private void RunMovement()
    165.         {
    166.             var axis = new Vector2(
    167.                 (_leftInput.IsPressed ? 0 : 0) + (_rightInput.IsPressed ? 0 : 0),
    168.  
    169.  
    170.  
    171.                 (_leftInput.IsPressed ? 0 : 0) + (_runInput.IsPressed ? 2 : 0)
    172.             ).normalized;
    173.  
    174.          
    175.  
    176.  
    177.  
    178.  
    179.  
    180.  
    181.             var speed = _realGrounded ? Accelaration : airMovementSpeed;
    182.        
    183.  
    184.  
    185.             var vertical = axis.y * speed * Time.fixedDeltaTime * transform.forward;
    186.  
    187.          
    188.  
    189.  
    190.                 var horizontal = axis.x * speed * Time.fixedDeltaTime * transform.right;
    191.  
    192.        
    193.  
    194.             if (CanApplyForce(vertical, axis))
    195.                 playerRigidBody.velocity += vertical;
    196.  
    197.        
    198.  
    199.             if (CanApplyForce(horizontal, axis))
    200.                 playerRigidBody.velocity += horizontal;
    201.        
    202.         }
    203.  
    204.         private void ApplyFriction() {
    205.             var vel = playerRigidBody.velocity;
    206.             var target = _realGrounded ? friction : airFriction;
    207.             vel.x = Mathf.Lerp(vel.x, 0f, target * Time.fixedDeltaTime);
    208.             vel.z = Mathf.Lerp(vel.z, 0f, target * Time.fixedDeltaTime);
    209.             playerRigidBody.velocity = vel;
    210.         }
    211.      
    212.    
    213.  
    214.         private void ApplyGravity() {
    215.             var vel = playerRigidBody.velocity;
    216.             vel.y -= Mathf.Abs(vel.y) * Time.fixedDeltaTime * extraGravity;
    217.             playerRigidBody.velocity = vel;
    218.         }
    219.  
    220.         private void Jumping()
    221.         {
    222.  
    223.             if (Run==true)
    224.          
    225. {
    226.  
    227.  
    228.                 _jumpCooldown -= Time.deltaTime;
    229.                 if (!(_grounded >= 0) || !(_jumpCooldown <= 0) || !_jumpInput.IsDown) return;
    230.  
    231.                 var vel = playerRigidBody.velocity;
    232.                 vel.y = jumpForce;
    233.                 playerRigidBody.velocity = vel;
    234.                 _jumpCooldown = jumpCooldown;
    235.  
    236.             }
    237. }
    238.  
    239.      
    240.         private bool CanApplyForce(Vector3 target, Vector2 axis) {
    241.             var targetC = Get2DVec(target).normalized;
    242.             var velocityC = Get2DVec(playerRigidBody.velocity).normalized;
    243.             var dotProduct = Vector2.Dot(velocityC, targetC);
    244.             return dotProduct <= 0 || dotProduct * Get2DVec(playerRigidBody.velocity).magnitude < maxSpeed * GetAxisForce(axis);
    245.         }
    246.  
    247.         private static float GetAxisForce(Vector2 axis) {
    248.             return (int)axis.x != 0 ? Mathf.Abs(axis.x) : Mathf.Abs(axis.y);
    249.         }
    250.  
    251.         private static Vector2 Get2DVec(Vector3 vec) {
    252.             return new Vector2(vec.x, vec.z);
    253.         }
    254.     }
    255. }
    256.  
     
    Last edited: Jul 4, 2020