Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question I cant jump on any terrain

Discussion in 'Scripting' started by Afterlife301209, Jan 15, 2022.

  1. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    44
    I was trying to create a FPS controller, But when I try to jump on uneven terrain or any terrain as a matter of fact, It doesn't jump. Is something wrong with my Code or is it something else? Please help!
    here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     float playerHeight = 2f;
    8.  
    9.     [SerializeField] Transform orientation;
    10.  
    11.     [Header("Movement")]
    12.     [SerializeField] float moveSpeed = 6f;
    13.     [SerializeField] float airMultiplier = 0.4f;
    14.     float movementMultiplier = 10f;
    15.  
    16.     [Header("Sprinting")]
    17.     [SerializeField] float walkSpeed = 4f;
    18.     [SerializeField] float sprintSpeed = 6f;
    19.     [SerializeField] float acceleration = 10f;
    20.  
    21.     [Header("Jumping")]
    22.     public float jumpForce = 5f;
    23.  
    24.     [Header("Keybinds")]
    25.     [SerializeField] KeyCode jumpKey = KeyCode.Space;
    26.     [SerializeField] KeyCode sprintKey = KeyCode.LeftShift;
    27.  
    28.     [Header("Drag")]
    29.     [SerializeField] float groundDrag = 6f;
    30.     [SerializeField] float airDrag = 2f;
    31.  
    32.     float horizontalMovement;
    33.     float verticalMovement;
    34.  
    35.     [Header("Ground Detection")]
    36.     [SerializeField] Transform groundCheck;
    37.     [SerializeField] LayerMask groundMask;
    38.     [SerializeField] float groundDistance = 0.2f;
    39.     public bool isGrounded { get; private set; }
    40.  
    41.     Vector3 moveDirection;
    42.     Vector3 slopeMoveDirection;
    43.  
    44.     Rigidbody rb;
    45.  
    46.     RaycastHit slopeHit;
    47.  
    48.     private bool OnSlope()
    49.     {
    50.         if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
    51.         {
    52.             if (slopeHit.normal != Vector3.up)
    53.             {
    54.                 return true;
    55.             }
    56.             else
    57.             {
    58.                 return false;
    59.             }
    60.         }
    61.         return false;
    62.     }
    63.  
    64.     private void Start()
    65.     {
    66.         rb = GetComponent<Rigidbody>();
    67.         rb.freezeRotation = true;
    68.     }
    69.  
    70.     private void Update()
    71.     {
    72.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    73.  
    74.         MyInput();
    75.         ControlDrag();
    76.         ControlSpeed();
    77.  
    78.         if (Input.GetKeyDown(jumpKey) && isGrounded)
    79.         {
    80.             Jump();
    81.         }
    82.  
    83.         slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
    84.     }
    85.  
    86.     void MyInput()
    87.     {
    88.         horizontalMovement = Input.GetAxisRaw("Horizontal");
    89.         verticalMovement = Input.GetAxisRaw("Vertical");
    90.  
    91.         moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
    92.     }
    93.  
    94.     void Jump()
    95.     {
    96.         if (isGrounded)
    97.         {
    98.             rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
    99.             rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    100.         }
    101.     }
    102.  
    103.     void ControlSpeed()
    104.     {
    105.         if (Input.GetKey(sprintKey) && isGrounded)
    106.         {
    107.             moveSpeed = Mathf.Lerp(moveSpeed, sprintSpeed, acceleration * Time.deltaTime);
    108.         }
    109.         else
    110.         {
    111.             moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
    112.         }
    113.     }
    114.  
    115.     void ControlDrag()
    116.     {
    117.         if (isGrounded)
    118.         {
    119.             rb.drag = groundDrag;
    120.         }
    121.         else
    122.         {
    123.             rb.drag = airDrag;
    124.         }
    125.     }
    126.  
    127.     private void FixedUpdate()
    128.     {
    129.         MovePlayer();
    130.     }
    131.  
    132.     void MovePlayer()
    133.     {
    134.         if (isGrounded && !OnSlope())
    135.         {
    136.             rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
    137.         }
    138.         else if (isGrounded && OnSlope())
    139.         {
    140.             rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
    141.         }
    142.         else if (!isGrounded)
    143.         {
    144.             rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
    145.         }
    146.     }
    147. }
    and here is a screen shot :
    upload_2022-1-15_14-8-1.png
    Thanks!
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    364
  3. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    44
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    2,809
    Are you sure? Have you Debug.Log()-ed it, or are you just assuming?

    If you debug it and find that it's returning false all the time, then you know there's something up with your CheckSphere. If not, something else is wrong, such as your jump method not applying enough force.
     
  5. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    44
    the debug is returning false
     
  6. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    44
    I definitely Debug.Log()-ed it
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     float playerHeight = 2f;
    8.  
    9.     [SerializeField] Transform orientation;
    10.  
    11.     [Header("Movement")]
    12.     [SerializeField] float moveSpeed = 6f;
    13.     [SerializeField] float airMultiplier = 0.4f;
    14.     float movementMultiplier = 10f;
    15.  
    16.     [Header("Sprinting")]
    17.     [SerializeField] float walkSpeed = 4f;
    18.     [SerializeField] float sprintSpeed = 6f;
    19.     [SerializeField] float acceleration = 10f;
    20.  
    21.     [Header("Jumping")]
    22.     public float jumpForce = 5f;
    23.  
    24.     [Header("Keybinds")]
    25.     [SerializeField] KeyCode jumpKey = KeyCode.Space;
    26.     [SerializeField] KeyCode sprintKey = KeyCode.LeftShift;
    27.  
    28.     [Header("Drag")]
    29.     [SerializeField] float groundDrag = 6f;
    30.     [SerializeField] float airDrag = 2f;
    31.  
    32.     float horizontalMovement;
    33.     float verticalMovement;
    34.  
    35.     [Header("Ground Detection")]
    36.     [SerializeField] Transform groundCheck;
    37.     [SerializeField] LayerMask groundMask;
    38.     [SerializeField] float groundDistance = 0.2f;
    39.     public bool isGrounded { get; private set; }
    40.  
    41.     Vector3 moveDirection;
    42.     Vector3 slopeMoveDirection;
    43.  
    44.     Rigidbody rb;
    45.  
    46.     RaycastHit slopeHit;
    47.  
    48.     private bool OnSlope()
    49.     {
    50.         if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
    51.         {
    52.             if (slopeHit.normal != Vector3.up)
    53.             {
    54.                 return true;
    55.             }
    56.             else
    57.             {
    58.                 return false;
    59.             }
    60.         }
    61.         return false;
    62.     }
    63.  
    64.     public void GroundCheckDebug()
    65.     {
    66.         if (isGrounded)
    67.         {
    68.             Debug.Log("Grounded");
    69.         }
    70.     }
    71.  
    72.  
    73.     private void Start()
    74.     {
    75.         rb = GetComponent<Rigidbody>();
    76.         rb.freezeRotation = true;
    77.     }
    78.  
    79.     private void Update()
    80.     {
    81.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    82.  
    83.         MyInput();
    84.         ControlDrag();
    85.         ControlSpeed();
    86.  
    87.         if (Input.GetKeyDown(jumpKey) && isGrounded)
    88.         {
    89.             Jump();
    90.         }
    91.  
    92.         slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
    93.         GroundCheckDebug();
    94.     }
    95.  
    96.     void MyInput()
    97.     {
    98.         horizontalMovement = Input.GetAxisRaw("Horizontal");
    99.         verticalMovement = Input.GetAxisRaw("Vertical");
    100.  
    101.         moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
    102.     }
    103.  
    104.     void Jump()
    105.     {
    106.         if (isGrounded)
    107.         {
    108.             rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
    109.             rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    110.         }
    111.     }
    112.  
    113.     void ControlSpeed()
    114.     {
    115.         if (Input.GetKey(sprintKey) && isGrounded)
    116.         {
    117.             moveSpeed = Mathf.Lerp(moveSpeed, sprintSpeed, acceleration * Time.deltaTime);
    118.         }
    119.         else
    120.         {
    121.             moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
    122.         }
    123.     }
    124.  
    125.     void ControlDrag()
    126.     {
    127.         if (isGrounded)
    128.         {
    129.             rb.drag = groundDrag;
    130.         }
    131.         else
    132.         {
    133.             rb.drag = airDrag;
    134.         }
    135.     }
    136.  
    137.     private void FixedUpdate()
    138.     {
    139.         MovePlayer();
    140.     }
    141.  
    142.     void MovePlayer()
    143.     {
    144.         if (isGrounded && !OnSlope())
    145.         {
    146.             rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
    147.         }
    148.         else if (isGrounded && OnSlope())
    149.         {
    150.             rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
    151.         }
    152.         else if (!isGrounded)
    153.         {
    154.             rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
    155.         }
    156.     }
    157. }
     
  7. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    44
    So now what?
     
  8. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    364
    moveDirection is a Vector3 being assigned from an expression using single floats in MyInput(); I would investigate it further to see what moveDirection is becoming in this instance.
    Code (CSharp):
    1. moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
     
  9. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    44
    do you think there is somthing up with my terrain?
     
  10. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    44
    Yep I set my terrain as 'Ground layer' and then i could jump just fine :mad:
     
  11. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    44
    Thanks everyone for you help!
     
unityunity