I am completely beside myself about what's happening here. The IsGrounded is not working as I want it to and the velocity goes berserk when I try moving. Why is this happening and how do I fix this?
Code:
public class ShipPhysics : MonoBehaviour
{
public float MaxSpeed;
public float Speed;
public float MinSpeed;
public float Acceleration;
public float Deceleration;
public float Handling;
public Rigidbody rb;
public float GravitySpeed;
public bool IsGrounded;
public bool IsGrounded2;
public float Floatheight;
public float Drag;
int LayerCoolmask;
int LayerCoolmask2;
private void Awake()
{
LayerCoolmask = LayerMask.GetMask("Ground");
LayerCoolmask = LayerMask.GetMask("Maglock");
}
void FixedUpdate()
{
IsGrounded = Physics.Raycast(transform.position, -transform.up, Floatheight, LayerCoolmask);
IsGrounded2 = Physics.Raycast(transform.position, -transform.up, Floatheight, LayerCoolmask2);
if (!IsGrounded2 && !IsGrounded)
{
GravitySpeed += Physics.gravity.y * Time.deltaTime;
}
else
{
GravitySpeed = 0;
}
if (Input.GetAxis("Vertical") > 0)
{
Speed += Acceleration * Input.GetAxis("Vertical") * Time.deltaTime;
}
if (Input.GetAxis("Vertical") < 0)
{
Speed += Deceleration * Input.GetAxis("Vertical") * Time.deltaTime;
}
else
{
Speed -= Drag * Time.deltaTime;
}
if (Speed > MaxSpeed)
Speed = MaxSpeed;
if (Speed < MinSpeed)
Speed = MinSpeed;
rb.angularVelocity = transform.right * Input.GetAxis("Horizontal") * Handling * Mathf.Deg2Rad;
rb.velocity = (transform.forward * Speed) - (Vector3.down * GravitySpeed);
}
}```
