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);
}
}