When the game runs the player has a low speed, but the speed slowly increeses. When it moves slow collisions are detected perfectly, but when the speed it’s higher collisions are detected late so the player stops after the collision.
This is my collision code:
public class PCollision : MonoBehaviour {
public Movement movement;
public Rigidbody rb;
public bool jumpAvailable, rebote;
public float fowardForce;
private void OnCollisionEnter(UnityEngine.Collision collisionInfo)
{
if (collisionInfo.gameObject.tag == "Obstacle")
{
rb.constraints = RigidbodyConstraints.FreezePosition;
rb.constraints = RigidbodyConstraints.None;
movement.enabled = false;
if (rebote == true)
{
rb.AddForce(0, 0, -fowardForce * Time.deltaTime);
rebote = false;
}
}
This is my movement code:
public class Movement : MonoBehaviour {
public Rigidbody rb;
public Transform tr;
public float fowardForce, sidewaysForce, jumpForce;
public bool jumpAvailable = true;
// Update is called once per frame
void FixedUpdate()
{
// This makes the player run
rb.AddForce(0, 0, fowardForce * Time.deltaTime);
PCollision variable = GetComponent<PCollision>();
jumpAvailable = variable.jumpAvailable;
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (jumpAvailable == true)
{
if (Input.GetKeyDown("w"))
{
rb.AddForce(0, jumpForce * Time.deltaTime, 0, ForceMode.Impulse);
}
}
}
}