Search Unity

Character didn't jump on Walking

Discussion in 'Scripting' started by AsoraX, Nov 28, 2017.

  1. AsoraX

    AsoraX

    Joined:
    May 3, 2017
    Posts:
    22
    Hey Guys,

    crr. i had the problem that my CharacterController ist not Jumping when you walk (runspeed = 1)...
    If you Run (rs = 2) or stand in idle (rs = 0) it works.

    Maybe anyone can take a look inside and can help me with the soluton ;)


    Thanks a lot


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Rigidbody))]
    6. [RequireComponent(typeof(CapsuleCollider))]
    7. [RequireComponent(typeof(Animator))]
    8. public class NewController : MonoBehaviour
    9. {
    10.     Rigidbody myRigidbody;
    11.     Animator myAnimator;
    12.     CapsuleCollider myCapsule;
    13.  
    14.     float forwardAmount;
    15.     [SerializeField] float runCycleLegOffset = 0.2f;
    16.  
    17.     #region InputSettings
    18.     public float vertical;
    19.     public float horizontal;
    20.     #endregion
    21.  
    22.     #region MovementSettings
    23.     //public bool run = false;
    24.     [SerializeField] public float runspeed = 0;
    25.  
    26.     [Range(1f, 4f)] [SerializeField] float GravityMultiplier = 2f;
    27.     public float JumpPower = 6f;
    28.     #endregion
    29.  
    30.     #region StateSettings
    31.     public bool isCrouching = false;
    32.     public bool isJumping = false;
    33.  
    34.     [SerializeField] public float distanceToGround = 0.1f;
    35.     [SerializeField] public float groundCheckDistance = 0.2f;
    36.     public bool isGrounded;
    37.  
    38.     float CapsuleHeight;
    39.     Vector3 CapsuleCenter;
    40.     const float k_Half = 0.5f;
    41.     #endregion
    42.  
    43.     #region Debug
    44.     public float extraGravityForce;
    45.     #endregion Debug
    46.  
    47.     #region CameraControll
    48.  
    49.     private float mouseX, mouseY;
    50.     public float mouseSensitivity = 10f;
    51.     public float mouseYPosition = 1f;
    52.  
    53.     private float zoom;
    54.     public float zoomSpeed = 2;
    55.  
    56.     public float zoomMin = -2f;
    57.     public float zoomMax = -10f;
    58.  
    59.     public float rotationSpeed = 5f;
    60.  
    61.     public Transform playerCamera, playerCharacter, centerPoint;
    62.     #endregion CameraControll
    63.  
    64.  
    65.     // Use this for initialization
    66.     void Start()
    67.     {
    68.         myAnimator = GetComponent<Animator>();
    69.         myRigidbody = GetComponent<Rigidbody>();
    70.         myCapsule = GetComponent<CapsuleCollider>();
    71.         CapsuleHeight = myCapsule.height;
    72.         CapsuleCenter = myCapsule.center;
    73.  
    74.         myRigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
    75.  
    76.         zoom = -4;
    77.     }
    78.  
    79.     // Update is called once per frame
    80.     void Update()
    81.     {
    82.  
    83.         zoom += Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
    84.  
    85.         if (zoom > zoomMin)
    86.             zoom = zoomMin;
    87.  
    88.         if (zoom < zoomMax)
    89.             zoom = zoomMax;
    90.  
    91.         playerCamera.transform.localPosition = new Vector3(0, 0, zoom);
    92.  
    93.         mouseX += Input.GetAxis("Mouse X");
    94.         mouseY -= Input.GetAxis("Mouse Y");
    95.  
    96.         mouseY = Mathf.Clamp(mouseY, -60f, 60f);
    97.         playerCamera.LookAt(centerPoint);
    98.         centerPoint.localRotation = Quaternion.Euler(mouseY, mouseX, 0);
    99.         centerPoint.position = new Vector3(playerCharacter.position.x, playerCharacter.position.y + mouseYPosition, playerCharacter.position.z);
    100.  
    101.         CheckGroundStatus();
    102.         Move();
    103.         Crouch();
    104.         Jump();
    105.  
    106.         if (!isGrounded)
    107.         {
    108.             myAnimator.SetFloat("Velocity", myRigidbody.velocity.y);
    109.         }
    110.  
    111.         if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
    112.         {
    113.             Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
    114.             playerCharacter.rotation = Quaternion.Slerp(playerCharacter.rotation, turnAngle, Time.deltaTime * rotationSpeed);
    115.         }
    116.  
    117.         forwardAmount = vertical;
    118.         float runCycle = Mathf.Repeat(myAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime + runCycleLegOffset, 1);
    119.         float jumpLeg = (runCycle < k_Half ? 1 : -1) * forwardAmount;
    120.         if (isGrounded)
    121.         {
    122.             myAnimator.SetFloat("JumpLeg", jumpLeg);
    123.         }
    124.     }
    125.  
    126.     private void FixedUpdate()
    127.     {
    128.         vertical = Input.GetAxis("Vertical");
    129.         horizontal = Input.GetAxis("Horizontal");
    130.     }
    131.  
    132.     void Move()
    133.     {
    134.         if (Input.GetKey(KeyCode.LeftShift) && !isCrouching)
    135.         {
    136.             if (runspeed < 2 && vertical != 0)
    137.             {
    138.                 runspeed = runspeed + 2.5f * Time.deltaTime;
    139.             }
    140.         }
    141.         else
    142.         {
    143.             if (runspeed > 1 && vertical != 0)
    144.             {
    145.                 runspeed = runspeed - 1.5f * Time.deltaTime;
    146.             }
    147.             else
    148.             {
    149.                 runspeed = 1;
    150.             }
    151.         }
    152.         myAnimator.SetFloat("Speed", runspeed * vertical);
    153.         myAnimator.SetFloat("Direction", runspeed * horizontal);
    154.     }
    155.  
    156.     void Crouch()
    157.     {
    158.         if (Input.GetKeyDown(KeyCode.C))
    159.         {
    160.             isCrouching = !isCrouching;
    161.         }
    162.         if (isCrouching)
    163.         {
    164.             if (vertical == 0 && horizontal == 0)
    165.             {
    166.                 myCapsule.height = CapsuleHeight / 1.7f;
    167.                 myCapsule.center = CapsuleCenter / 1.7f;
    168.             }
    169.             else
    170.             {
    171.                 myCapsule.height = CapsuleHeight / 1.5f;
    172.                 myCapsule.center = CapsuleCenter / 1.5f;
    173.                 Debug.Log("Crouch");
    174.             }
    175.         }
    176.         else
    177.         {
    178.             myCapsule.height = CapsuleHeight;
    179.             myCapsule.center = CapsuleCenter;
    180.  
    181.             Ray crouchRay = new Ray(myRigidbody.position + Vector3.up * myCapsule.radius * k_Half, Vector3.up);
    182.             float crouchRayLength = CapsuleHeight - myCapsule.radius * k_Half;
    183.             if (Physics.SphereCast(crouchRay, myCapsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
    184.             {
    185.                 isCrouching = true;
    186.                 return;
    187.             }
    188.             else
    189.             {
    190.                 myCapsule.height = CapsuleHeight;
    191.                 myCapsule.center = CapsuleCenter;
    192.             }
    193.         }
    194.         myAnimator.SetBool("isCrouching", isCrouching);
    195.     }
    196.  
    197.     void Jump()
    198.     {
    199.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded != false)
    200.         {
    201.             myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, JumpPower, myRigidbody.velocity.z);
    202.  
    203.             //DEBUG
    204.             Debug.Log("Space pressed...");
    205.             Debug.Log("New Vector: " + myRigidbody.velocity.x + ", " + JumpPower + ", " + myRigidbody.velocity.z);
    206.         }
    207.     }
    208.  
    209.     void CheckGroundStatus()
    210.     {
    211.         RaycastHit hitInfo;
    212.  
    213.         if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo))
    214.         {
    215.             distanceToGround = hitInfo.distance;
    216.         }
    217.  
    218.         if (distanceToGround <= 0.2f)
    219.         {
    220.             isGrounded = true;
    221.             myAnimator.applyRootMotion = true;
    222.         }
    223.         else
    224.         {
    225.             isGrounded = false;
    226.             myAnimator.applyRootMotion = false;
    227.         }
    228.  
    229.         myAnimator.SetBool("isGrounded", isGrounded);
    230.     }
    231.  
    232.     void HandleAirborneMovement()
    233.     {
    234.         // apply extra gravity from multiplier:
    235.         Vector3 extraGravityForce = (Physics.gravity * GravityMultiplier) - Physics.gravity;
    236.         myRigidbody.AddForce(extraGravityForce);
    237.  
    238.         Debug.Log(extraGravityForce);
    239.     }
    240. }
    241.