Given this script, I used it on a cylinder moving across a field (it is suspended in the air, without gravity influence). Four static cubic colliders act as walls, defining a simple rectangular bound.
The cylinder has a capsule collider and a physics material with bounciness 1. It has a small mass (0.1) and a little drag (0.01).
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private float x;
private float z;
public float speed=1200f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
x= Input.GetAxis("Horizontal")*speed*Time.deltaTime;
z= Input.GetAxis("Vertical")*speed*Time.deltaTime;
if (Mathf.Abs(rigidbody.velocity.x)<90&&Mathf.Abs(rigidbody.velocity.y)<90)
{
rigidbody.AddForce (x,0,z);
}
}
void Update (){
print(rigidbody.velocity);
}
}
Problem is, with speed set at 1200 (which is fast and reactive, considering the Time.deltaTime multiplayer), the collider often fails, making the cylinder pass through the wall. I tried to divide the fixed timestamp by half, with no result. The object is quite fast, but it’s certainly not a bullet (it is meant to be controlled by the player), so these errors seem quite strange to me.
Is there anything else I can do to improve the collision detection? I tried to set the rigidbody parameter as Continuous Dynamic, with no results whatsoever.