Search Unity

can't make player jump on slope

Discussion in 'Scripting' started by brunoenvia, Sep 19, 2021.

  1. brunoenvia

    brunoenvia

    Joined:
    Aug 5, 2019
    Posts:
    94
    why i cant jump when im on a slope? i get all the conditions needed for my IF to jump, and i don't jump for some reason
    slope function line 219
    and line 72 is when i should jump if i am on a slope

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.  
    9.  
    10.     // jump button UI event
    11.     public JumpButtonEvent jumpBtn;
    12.  
    13.     // player game object components
    14.     public AudioSource ballBounceSFX;
    15.     public Rigidbody playerRb;
    16.     public Collider playerCollider;
    17.  
    18.     // camera
    19.     public Transform gameCamera;
    20.  
    21.     // movement properties
    22.     public Vector3 playerVector;
    23.     public float movementSpeed;
    24.     public float jumpSpeed;
    25.     public float jumpForce = 50f;
    26.     public bool isJumping = false;
    27.     public bool isMoving = false;
    28.     public bool allowedToJump;
    29.     public FixedJoystick joystickMove;
    30.     public float x;
    31.     public float y;
    32.     public float z;
    33.     public float gravity = 2f;
    34.  
    35.     // raycast
    36.  
    37.     bool onSlope;
    38.     public bool isGrounded = false;
    39.     RaycastHit hitInfo;
    40.     RaycastHit slopeHit;
    41.     public LayerMask raycastLayers;
    42.     public float sphereRadius = 0.1f;
    43.     public float castDistance = 0.01f;
    44.     public float slopeCastDistance = 1f;
    45.     public float slopeSpeed = 1f;
    46.  
    47.  
    48.  
    49.     private void Awake()
    50.     {
    51.  
    52.     }
    53.  
    54.     // Start is called before the first frame update
    55.     void Start()
    56.     {
    57.  
    58.     }
    59.  
    60.     public void MovementAndJump()
    61.     {
    62.         Quaternion cameraRot = Quaternion.Euler(0, gameCamera.eulerAngles.y, 0); // convers camera quaternion to euler, to be able to rotate the camera in euler angles (rotation axis from inspector)
    63.  
    64.  
    65.  
    66.         x = joystickMove.Horizontal;
    67.         z = joystickMove.Vertical;
    68.  
    69.         playerVector = cameraRot * new Vector3(x, 0, z) * movementSpeed; // the playervector input will have the x and z values. y is added later
    70.  
    71.         // jumping
    72.         if (isJumping == false && jumpBtn.jumpBtnClicked == true && (IsGrounded() == true || OnSlope() == true))
    73.         {          
    74.             Vector3 jumpVector = new Vector3(0, jumpForce, 0);
    75.             playerRb.AddForce(jumpVector, ForceMode.Impulse);
    76.             isJumping = true;
    77.         }
    78.  
    79.  
    80.         if (isJumping == true && IsGrounded() == false)
    81.         {
    82.             playerVector.y = playerVector.y - gravity; // if player is mid-air (jumping) the vertical movement will be subtracted to the gravity, making the gravity pushing the player to the ground          
    83.         }
    84.  
    85.  
    86.  
    87.         if (IsGrounded() == true && isMoving == true)
    88.         {
    89.             playerRb.drag = 15f;
    90.         }
    91.  
    92.  
    93.  
    94.  
    95.  
    96.         if (isMoving == false && IsGrounded() == true)
    97.         {
    98.             playerRb.velocity = Vector3.zero;
    99.             playerRb.angularVelocity = Vector3.zero;
    100.             playerRb.drag = 0f;
    101.         }
    102.  
    103.  
    104.  
    105.  
    106.  
    107.         if (isJumping == true)
    108.         {
    109.             playerRb.drag = 0f;
    110.         }
    111.  
    112.  
    113.  
    114.  
    115.  
    116.         if (isJumping == true && isMoving == true)
    117.         {
    118.             playerRb.velocity = new Vector3(playerVector.x * jumpSpeed, playerRb.velocity.y, playerVector.z * jumpSpeed); // when jumping will create a new vector that will move according to the value on playerVector.x and playerVector.z multiplying by a moving speed on jump
    119.         }
    120.  
    121.  
    122.  
    123.         // moving bools
    124.         if (joystickMove.Horizontal == 0 && joystickMove.Vertical == 0)
    125.         {
    126.             isMoving = false;
    127.         }
    128.  
    129.         else
    130.         {
    131.             isMoving = true;
    132.  
    133.         }
    134.  
    135.  
    136.         // moving rigidbody (player)
    137.  
    138.         playerRb.AddForce(playerVector, ForceMode.VelocityChange);
    139.  
    140.  
    141.  
    142.  
    143.  
    144.  
    145.  
    146.     }
    147.  
    148.     // Update is called once per frame
    149.     void Update()
    150.     {
    151.         Debug.Log("Is player on a slope?" + OnSlope());
    152.         Debug.Log("Is player Jumping?" + isJumping);
    153.         Debug.Log("Is player grounded?" + IsGrounded());
    154.     }
    155.  
    156.     private void FixedUpdate()
    157.     {
    158.         IsGrounded();
    159.         MovementAndJump();
    160.  
    161.  
    162.     }
    163.  
    164.     public bool IsGrounded() // cria uma funçao bool que verifica se o player está no chão ou não
    165.     {
    166.         if (Physics.SphereCast(playerCollider.bounds.center, sphereRadius, Vector3.down, out hitInfo, castDistance, raycastLayers))
    167.         {
    168.             Debug.Log(hitInfo.collider.name);
    169.  
    170.             if (hitInfo.collider.gameObject.layer == 7 || hitInfo.collider.gameObject.layer == 8) // se o layer do objeto que o raycast bateu for 7(Ground) então retorna um valor TRUE(significando que o play esta no chao)
    171.             {
    172.  
    173.                 gameObject.transform.parent = null;
    174.                 isJumping = false;
    175.                 return true;
    176.             }
    177.  
    178.             else if (hitInfo.collider.gameObject.layer == 6)
    179.             {
    180.                 gameObject.transform.SetParent(hitInfo.collider.transform, true);
    181.                 isJumping = false;
    182.                 return true;
    183.             }
    184.  
    185.             else if (hitInfo.collider == null) // se o raycast nao detetar qualquer collider, o player nao está no chao
    186.             {
    187.                 Gizmos.color = Color.red;
    188.                 gameObject.transform.parent = null;
    189.                 isJumping = true;
    190.  
    191.                 return false;
    192.             }
    193.  
    194.             else if (OnSlope() == true)
    195.             {
    196.                 isJumping = false;
    197.             }
    198.  
    199.             else // se o raycast nao detetar qualquer collider, o player nao está no chao
    200.             {
    201.                 gameObject.transform.parent = null;
    202.                 isJumping = true;
    203.  
    204.                 return false;
    205.             }
    206.         }
    207.  
    208.  
    209.         gameObject.transform.parent = null;
    210.         isJumping = true;
    211.  
    212.  
    213.         return false; // if the player isn't touching the ground he is jumping and not on any platform
    214.  
    215.  
    216.     }
    217.  
    218.  
    219.     public bool OnSlope()
    220.     {
    221.         if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, raycastLayers))
    222.         {
    223.             if (slopeHit.normal != Vector3.up)
    224.             {
    225.                 isJumping = false;
    226.                 return true;
    227.             }
    228.         }
    229.         isJumping = true;
    230.         return false;
    231.     }
    232.  
    233.     private void OnDrawGizmosSelected()
    234.     {
    235.         Gizmos.color = Color.red;
    236.         Gizmos.DrawRay(playerCollider.bounds.center, Vector3.down * castDistance);
    237.         Gizmos.DrawSphere(playerCollider.bounds.center, sphereRadius);
    238.  
    239.         Gizmos.color = Color.yellow;
    240.         Gizmos.DrawRay(transform.position, Vector3.down * slopeCastDistance);
    241.     }
    242.  
    243.  
    244.     // on collision frame
    245.     private void OnCollisionEnter(Collision collision)
    246.     {
    247.  
    248.  
    249.         if (collision.gameObject.layer == 6 && IsGrounded() == true || collision.gameObject.layer == 7 && IsGrounded() == true || collision.gameObject.layer == 8 && IsGrounded() == true) // if the player hits one of these layers when colliding with them on colliding frame
    250.         {
    251.             ballBounceSFX.Play(); // play the bouncing ball sfx
    252.         }
    253.     }
    254.  
    255. }
    debugs:
     
  2. brunoenvia

    brunoenvia

    Joined:
    Aug 5, 2019
    Posts:
    94
    nvm i think i figured out whats happening
    the grounded raycast when returns false i set jump to true so it might be because of that
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    This can be problematic for a variety of reasons. Here's one script with a timer solution:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller:

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    But that one has run, walk, jump, slide, crouch... it's crazy-nutty!!