I’ve tried different techniques to make this work but I haven’t figured it out so hopefully someone could help me tailor it to my code, thanks! I’m trying to make it so that my character can only run at a certain speed.
{
public Transform tr;
public Rigidbody rb;
public float maxspeed = 500f;
public float speed = 450f;
public float jumpspeed = 600f;
public GameObject player;
public bool grounded;
public GameObject Ground;
public Animation disappear;
public void Start()
{
grounded = true;
}
public void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.name == "Ground")
{
grounded = true;
}
}
public void OnCollisionExit(Collision theCollision)
{
if (theCollision.gameObject.name == "Ground")
{
grounded = false;
}
}
private void FixedUpdate()
{
if (rb.velocity.magnitude > maxspeed)
{
rb.velocity = rb.velocity.normalized * maxspeed;
}
if (Input.GetKeyDown("e"))
{ disappear["disappear"].speed = 1;
disappear.Play("disappear");
}
if (Input.GetKeyUp("e"))
{
disappear["disappear"].speed = -1;
disappear["disappear"].time = disappear["disappear"].length;
disappear.Play("disappear");
}
if (Input.GetKey("w") && grounded == true)
{
rb.AddForce(0, jumpspeed * Time.deltaTime,0);
}
if (Input.GetKey("a") && grounded == true)
{
rb.AddForce(-speed * Time.deltaTime,0,0);
}
if (Input.GetKey("d") && grounded == true)
{
rb.AddForce(speed * Time.deltaTime,0,0);
}
}
}