Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Gravity not working correctly

Discussion in 'Physics' started by Colby314, Jul 29, 2020.

  1. Colby314

    Colby314

    Joined:
    Jun 25, 2020
    Posts:
    3
    This is a character-controller based movement system. When I run the project at first (the player starts in the air), instead of falling normally it falls very slowly. The gravity dictates that it should fall down with a speed of -9.81, but it's more like -0.3 or so. The problem isn't that it falls too slow. The ground check system doesn't seem to work. The strange thing is that when the player hits the ground, isGrounded is set to true. However, the player continues by falling straight through the ground object and into the void. I can't figure out what's wrong. Here's the script (this also include some wall running stuff):

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.     public Camera playerCamera;
    10.     Quaternion playerCameraLocalRotation;
    11.  
    12.     //Ground check and basic movement
    13.     public CharacterController control;
    14.     public float gravity = -9.81f;
    15.     bool useGravity;
    16.     public static Vector3 vel;
    17.     public static bool isGrounded;
    18.     public Transform groundCheck;
    19.     public float groundDistance = 0.3f;
    20.     public float speed = 12f;
    21.     public float jumpHeight = 3f;
    22.     public LayerMask groundMask;
    23.     public float mouseSens = 100f;
    24.     private float xRotation = 0f;
    25.     private float desiredX;
    26.     public Transform playerBody;
    27.  
    28.     //Wallrunning
    29.     public LayerMask whatIsWall;
    30.     public float wallrunForce, maxWallrunTime, maxWallSpeed;
    31.     bool isWallRight, isWallLeft;
    32.     bool isWallRunning;
    33.     public static float maxWallRunCameraTilt, wallRunCameraTilt;
    34.     public Transform orientation;
    35.  
    36.     private void WallRunInput()
    37.     {
    38.         //Start the wallrun
    39.         if(Input.GetKey(KeyCode.D) && isWallRight)
    40.         {
    41.             StartWallRun();
    42.         }
    43.         if (Input.GetKey(KeyCode.A) && isWallLeft)
    44.         {
    45.             StartWallRun();
    46.         }
    47.     }
    48.  
    49.     private void StartWallRun()
    50.     {
    51.         useGravity = false;
    52.         isWallRunning = true;
    53.  
    54.         if(vel.magnitude <= maxWallSpeed)
    55.         {
    56.             vel += orientation.forward * wallrunForce * Time.deltaTime;
    57.  
    58.             if(isWallRight)
    59.             {
    60.                 vel += orientation.right * wallrunForce / 5 * Time.deltaTime;
    61.  
    62.             }
    63.             else
    64.             {
    65.                 vel += -orientation.right * wallrunForce / 5 * Time.deltaTime;
    66.             }
    67.         }
    68.     }
    69.  
    70.     private void StopWallRun()
    71.     {
    72.         useGravity = true;
    73.         isWallRunning = false;
    74.         vel = Vector3.zero;
    75.     }
    76.  
    77.     private void CheckForWall()
    78.     {
    79.         isWallRight = Physics.Raycast(transform.position, orientation.right, 1f, whatIsWall);
    80.         isWallLeft = Physics.Raycast(transform.position, -orientation.right, 1f, whatIsWall);
    81.  
    82.         if(!isWallLeft && !isWallRight)
    83.         {
    84.             StopWallRun();
    85.         }
    86.  
    87.     }
    88.  
    89.     // Start is called before the first frame update
    90.     void Start()
    91.     {
    92.         Cursor.lockState = CursorLockMode.Locked;
    93.         Cursor.visible = false;
    94.         vel.y = -9.81f;
    95.     }
    96.  
    97.     // Update is called once per frame
    98.     void Update()
    99.     {
    100.  
    101.         DoMouseLook();
    102.         CheckForWall();
    103.         WallRunInput();
    104.         DoGroundCheck();
    105.        
    106.  
    107.        
    108.  
    109.         float x = Input.GetAxis("Horizontal");
    110.         float z = Input.GetAxis("Vertical");
    111.  
    112.         if(Math.Abs(wallRunCameraTilt) < maxWallRunCameraTilt && isWallRunning && isWallRight)
    113.         {
    114.             wallRunCameraTilt += Time.deltaTime * maxWallRunCameraTilt * 2;
    115.         }
    116.         if(Math.Abs(wallRunCameraTilt) < maxWallRunCameraTilt && isWallRunning && isWallLeft)
    117.         {
    118.             wallRunCameraTilt -= Time.deltaTime * maxWallRunCameraTilt * 2;
    119.         }
    120.  
    121.         //Quaternion q;
    122.         //q = playerCamera.transform.localRotation;
    123.         playerCamera.transform.localRotation = Quaternion.Euler(xRotation, desiredX, wallRunCameraTilt);
    124.  
    125.         //Tilt camera back
    126.         if (wallRunCameraTilt > 0 && !isWallRight && !isWallLeft)
    127.         {
    128.             wallRunCameraTilt -= Time.deltaTime * maxWallRunCameraTilt * 2;
    129.         }
    130.         if(wallRunCameraTilt < 0 && !isWallRight && !isWallLeft)
    131.         {
    132.             wallRunCameraTilt += Time.deltaTime * maxWallRunCameraTilt * 2;
    133.         }
    134.  
    135.  
    136.         Vector3 move = transform.right * x + transform.forward * z;
    137.  
    138.  
    139.         control.Move(move * speed * Time.deltaTime);
    140.  
    141.         if(Input.GetButtonDown("Jump"))
    142.         {
    143.             Debug.Log("Button pressed, before calling Jump() ");
    144.             Jump();
    145.             Debug.Log("After called Jump()");
    146.         }
    147.  
    148.  
    149.         vel.y += gravity * Time.deltaTime;
    150.  
    151.  
    152.         //Need to multiply by Time.deltaTime again
    153.         control.Move(vel * Time.deltaTime);
    154.  
    155.  
    156.  
    157.         //Debug.Log("isGrounded = " + isGrounded);
    158.         int a = Time.frameCount;
    159.         float b = Time.realtimeSinceStartup;
    160.         //Debug.Log("Frame count: " + a);
    161.         //Debug.Log("Real time in seconds: " + b);
    162.         //Debug.Log("IsWallRunning : " + isWallRunning);
    163.         Debug.Log("Vel : " + vel);
    164.        
    165.     }
    166.  
    167.     public void Jump()
    168.     {
    169.        
    170.         if(isWallRunning)
    171.         {
    172.             //A normal jump
    173.             if(isWallLeft && !Input.GetKey(KeyCode.D) || isWallRight && !Input.GetKey(KeyCode.A))
    174.             {
    175.                 vel.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    176.             }
    177.  
    178.             //sideways walljump
    179.             if(isWallRight || isWallLeft && Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) {
    180.                 vel += -orientation.up * jumpHeight * 1f;
    181.             }
    182.             if(isWallRight && Input.GetKey(KeyCode.A))
    183.             {
    184.                 vel += -orientation.right * jumpHeight * 3.2f;
    185.             }
    186.             if(isWallLeft && Input.GetKey(KeyCode.D))
    187.             {
    188.                 vel += orientation.right * jumpHeight * 3.2f;
    189.             }
    190.  
    191.             vel += orientation.forward * jumpHeight * 1f;
    192.  
    193.             //reset vel
    194.             vel = Vector3.zero;
    195.  
    196.  
    197.  
    198.             return;
    199.         }
    200.  
    201.         if (isGrounded)
    202.         {
    203.             vel.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    204.         }
    205.     }
    206.  
    207.     private void DoMouseLook()
    208.     {
    209.         float mouseX = Input.GetAxis("Mouse X") * mouseSens * Time.fixedDeltaTime;
    210.         float mouseY = Input.GetAxis("Mouse Y") * mouseSens * Time.fixedDeltaTime;
    211.  
    212.         //Find current look rotation
    213.         Vector3 rot = playerCamera.transform.localRotation.eulerAngles;
    214.         desiredX = rot.y + mouseX;
    215.  
    216.        
    217.         xRotation -= mouseY;
    218.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    219.  
    220.         //Perform the rotations
    221.         playerCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, wallRunCameraTilt);
    222.         playerBody.Rotate(Vector3.up * mouseX);
    223.     }
    224.  
    225.     private void DoGroundCheck()
    226.     {
    227.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    228.  
    229.  
    230.  
    231.  
    232.  
    233.         if (isGrounded && vel.y < 0)
    234.         {
    235.             //Slightly less than zero to ensure that the player actually lands on the ground and doesn't hover above it
    236.             vel.y = -0.1f;
    237.            
    238.         }
    239.         else if (isWallRunning)
    240.         {
    241.             vel.y = -0.2f;
    242.         }
    243.         else if (isGrounded)
    244.         {
    245.            
    246.         }
    247.     }
    248. }
    249.  
     
  2. Colby314

    Colby314

    Joined:
    Jun 25, 2020
    Posts:
    3
    Also, for some reason the transform.forward and transform.right arrows don't work correctly with the direction you're looking in.
     
  3. Colby314

    Colby314

    Joined:
    Jun 25, 2020
    Posts:
    3
    Actually, the gravity is fixed now. I just need help with the movement directions with the WASD keys actually doing what they're supposed to do.
     
unityunity