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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Player is completely stuck to the ground no matter what!

Discussion in 'Scripting' started by Carlitosjft, May 24, 2022.

  1. Carlitosjft

    Carlitosjft

    Joined:
    Mar 18, 2016
    Posts:
    7
    So I have been trying to make my character jump for days now, ive tried adding what you see inside the Jump action to the void update and it seems to jiggle him upwards but wont work when i press the jump key, the debug says it is being pressed it just does nothing!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. [RequireComponent(typeof(CharacterController))]
    7. [RequireComponent(typeof(PlayerInput))]
    8. public class TwinStickMovement : MonoBehaviour
    9. {
    10.     [SerializeField] private float playerSpeed = 5f;
    11.     //New
    12.     [SerializeField] private float jumpHeight = 20f;
    13.     private Rigidbody rb;
    14.     //New
    15.     [SerializeField] private float gravityValue = -4.81f;
    16.     [SerializeField] private float controllerDeadzone = 0.1f;
    17.     [SerializeField] private float gamepadRotateSmoothing = 1000f;
    18.  
    19.     [SerializeField] private bool isGamepad;
    20.  
    21.     private CharacterController controller;
    22.  
    23.     private Vector2 movement;
    24.     private Vector2 aim;
    25.     private float jump;
    26.     float velocity;
    27.  
    28.  
    29.  
    30.     private Vector3 playerVelocity;
    31.  
    32.     private PlayerControls playerControls;
    33.     private PlayerInput playerInput;
    34.  
    35.     private void Awake()
    36.     {
    37.         controller = GetComponent<CharacterController>();
    38.         playerControls = new PlayerControls();
    39.         playerInput = GetComponent<PlayerInput>();
    40.         rb = GetComponent<Rigidbody>();
    41.     }
    42.  
    43.     private void OnEnable()
    44.     {
    45.         playerControls.Enable();
    46.      
    47.     }
    48.  
    49.     private void OnDisable()
    50.     {
    51.         playerControls.Disable();
    52.      
    53.     }
    54.  
    55.     void Update()
    56.     {
    57.         HandleInput();
    58.         HandleMovement();
    59.         HandleRotation();
    60.         HandleJump();
    61.      
    62.  
    63.     }
    64.  
    65.     void HandleInput()
    66.     {
    67.         movement = playerControls.Controls.Movement.ReadValue<Vector2>();
    68.         aim = playerControls.Controls.Aim.ReadValue<Vector2>();
    69.         jump = playerControls.Controls.Jump.ReadValue<float>();
    70.     }
    71.  
    72.     void HandleMovement()
    73.     {
    74.         Vector3 move = new Vector3(movement.x, 0, movement.y);
    75.         controller.Move(move * Time.deltaTime * playerSpeed);
    76.  
    77.      
    78.     }
    79.  
    80.     void HandleRotation()
    81.     {
    82.         if (isGamepad)
    83.         {
    84.  
    85.             if (Mathf.Abs(aim.x) > controllerDeadzone || Mathf.Abs(aim.y) > controllerDeadzone)
    86.             {
    87.                 Vector3 playerDirection = Vector3.right * aim.x + Vector3.forward * aim.y;
    88.  
    89.                 if (playerDirection.sqrMagnitude > 0.0f)
    90.                 {
    91.                     Quaternion newrotation = Quaternion.LookRotation(playerDirection, Vector3.up);
    92.                     transform.rotation = Quaternion.RotateTowards(transform.rotation, newrotation, gamepadRotateSmoothing * Time.deltaTime);
    93.                 }
    94.             }
    95.         }
    96.         else
    97.         {
    98.             Ray ray = Camera.main.ScreenPointToRay(aim);
    99.             Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
    100.             float rayDistance;
    101.  
    102.             if (groundPlane.Raycast(ray, out rayDistance))
    103.             {
    104.                 Vector3 point = ray.GetPoint(rayDistance);
    105.                 LookAt(point);
    106.             }
    107.         }
    108.     }
    109.  
    110.    void HandleJump()
    111.     {
    112.         if (playerControls.Controls.Jump.triggered)
    113.         {
    114.             Debug.Log("We F***ing jumped");
    115.             rb.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
    116.             transform.Translate(new Vector3(0, jumpHeight, 0) * Time.deltaTime);
    117.             velocity = jumpHeight;
    118.         }
    119.         transform.Translate(new Vector3(0, velocity, 0) * Time.deltaTime);
    120.         rb.AddForce(Vector3.up * velocity, ForceMode.Impulse);
    121.         playerVelocity.y += gravityValue * Time.deltaTime;
    122.         controller.Move(playerVelocity * Time.deltaTime);
    123.     }
    124.  
    125.     private void LookAt(Vector3 LookPoint)
    126.     {
    127.         Vector3 heightCorrectedPoint = new Vector3(LookPoint.x, transform.position.y, LookPoint.z);
    128.         transform.LookAt(heightCorrectedPoint);
    129.     }
    130.  
    131.     public void OnDeviceChange (PlayerInput pi)
    132.     {
    133.         isGamepad = pi.currentControlScheme.Equals("Gamepad") ? true : false;
    134.     }
    135. }
    You can see Ive tried both rigidbody and non-physis and nothing works
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    Well, you're trying to both translate the transform, affect the rigid body, alongside move a CharacterController component. You can't and shouldn't be doing all three at the same time.

    Pick one and stick with it.
     
  3. Carlitosjft

    Carlitosjft

    Joined:
    Mar 18, 2016
    Posts:
    7
    Please give me an example, already picked one and couldnt make it work, how could i do this with the character controller?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    I'm not going to write the code for you, but to give you some direction, the CharacterController.Move is pretty simple: you give it a direction (a vector) and it goes in that direction at a speed based on the vector's magnitude.

    So the steps to take are:
    • Have a vector for the player's input
    • Have a vector keeping tabs of vertical velocity
    • Add these together at the end
    'Jumping' will just mean setting the vertical velocity to a positive value, and each frame you keep decreasing the y value in whatever manner works for your game.

    The final 'move' code will be pretty simple, such as this in a project of mine:
    Code (CSharp):
    1. public void ApplyMovement()
    2. {
    3.     if (entityController.enabled == true) entityController.Move((MovementVelocity + VerticalVelocity) * Time.deltaTime);
    4. }