Help With Player Input

Hi I was following some tutorials on basic game objects to get acquainted with Unity and c#. I am brand new to each. anyway I’ve been piecing together things beyond the original tutorial, but have run into some roadblocks I could not figure out simply by searching google.

Firstly I put a Lerp on my players velocity and angular velocity to quickly but gently bring it back to 0 when input was not detected. I did this so that the ball did not continue endlessly rolling off the map. It seems however that my methods have also reduced gravity’s affect of the ball. The ball does still fall when not in contact with the ground, but it is no accelerating at a simulated 9.8g*mass. Secondly, when the ball is in the air it seems I can still apply a forward force. I would like to restrict input to only times when the ball is in contact with the ground I was seeing and array of potential different methods, form rayTrace to Physics.colliderCapsule, the latter of the two was notated as being obsolete.

Lastly I also notice that my player, which is a ball, does not roll when moving, It’s more like it is just being pushed and sliding around instead of rolling.

anyway any advice on my endeavors would be greatly appreciated. Thank you!

using System;
using System.Diagnostics.Tracing;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.XR;

public class PlayerInput : MonoBehaviour
{
    public float speed = 20;
    public Rigidbody rb;
    public float horizontal;
    public float vertical;
    public float rotationSensitivity = 80;
    
    float pRotate = 0.0f;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

       // Update is called once per frame
    void Update()
    {
        pRotate += Input.GetAxis("Mouse X") * rotationSensitivity * Time.deltaTime;
        transform.eulerAngles = new Vector3(0.0f, pRotate);

        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

        Vector3 direction = new Vector3(horizontal, 0.0f, vertical);
        rb.AddRelativeForce(direction*speed);

                    
        if (Input.GetKeyUp(KeyCode.W))
        {
            Freeze();
        }

        if(Input.GetKeyUp(KeyCode.A))
        {
            Freeze();
        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            Freeze();
        }
        if (Input.GetKeyUp(KeyCode.D))
        {
            Freeze();
        }
        else
        {
            Freeze();
        }
    }

    void Freeze()
    {
    
        var val = rb.velocity;
        var vAr = rb.angularVelocity;
        Vector3 desiredVelocity = new Vector3(0f, 0f, 0f);
        Vector3 desiredAngularVelocity = new Vector3(0f, 0f, 0f);
        rb.velocity = Vector3.Lerp(val, desiredVelocity, 3f*Time.deltaTime);
        rb.angularVelocity = Vector3.Lerp(vAr, desiredAngularVelocity, 3f*Time.deltaTime);

    }
}

Your 3-axis Lerp is probably dulling the gravity’s effect on your velocity.

You could address it on an axis-by-axis basis, as long as you’re happy that gravity will only be on 1 axis, such as the Y axis.

Steps:

  • Copy the rb.velocity out to a temporary Vector3, say tempVelocity
  • Copy the tempVelocity.y field out to a temporary float, say holdYVelocity
  • apply your lerp-stop above to tempVelocity
  • copy the temporary float holdYVelocity back into the tempVelocity.y
  • assign tempVelocity back to rb.velocity

The copy out/copy back in is necessary because you cannot set rb.velocity.y directly.

Also Freeze() is running every frame no matter what due to the else statement.