- Home /
Collision isn't deteccted at a high speed
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);
}
}
}
}
Answer by Epsilon_Delta · Jun 01, 2019 at 11:26 AM
This is a common issue. You can try to set collision detection to continuous, continuous dynamic or continuous speculative (speculative is new feature available only in newer versions of Unity) on your rigidbodies or make your colliders bigger or combination of both. Or you can code your own workaround with raycasts. Further reading: https://docs.unity3d.com/Manual/ContinuousCollisionDetection.html
Your answer
Follow this Question
Related Questions
Check for collision while animating 0 Answers
object isn't detecting collisions 1 Answer
Destroying object on collision 3 Answers
Can I have a single rigidbody2d that acts as both collider and trigger? 1 Answer
2D Collision Detection in 3D Scene? 1 Answer