- Home /
Object Collider problem, objects going through walls?
Hello, I am making a game where tigers spawn and chase after you. I set up a scene where you are in a hotel-like place, all the walls are made out of unity cubes and therefore have box colliders set automatically. The tigers have rigidbodys and box colliders of their own. They contain this script to chase after the player: var target : Transform; var target : Transform;
var moveSpeed = 20;
var defMoveSpeed;
var rotationSpeed = 3;
static var reflex : boolean = false;
var myTransform : Transform;
var runningIntoWall : boolean = false;
function Awake()
{
myTransform = transform;
if (reflex){
moveSpeed *= 3;
rotationSpeed *= 3;
defMoveSpeed = moveSpeed;
}
if (!runningIntoWall){
defMoveSpeed = moveSpeed;
}
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
moveSpeed = defMoveSpeed;
}
function OnCollisionEnter(theCollision : Collision){
if (theCollision.gameObject.name == "Wall" || theCollision.gameObject.name == "2_Wall" || theCollision.gameObject.name == "3_Wall" || theCollision.gameObject.name == "4_Wall" || theCollision.gameObject.name == "2_Door" || theCollision.gameObject.name == "3_Door" || theCollision.gameObject.name == "4_Door" || theCollision.gameObject.name == "2_Room" || theCollision.gameObject.name == "3_Room" || theCollision.gameObject.name == "4_Room"){
moveSpeed = 0;
runningIntoWall = true;
}else {
moveSpeed = defMoveSpeed;
runningIntoWall = false;
}
}
However, often the tigers manage to break through the walls! Does anyone know how to fix this? Thanks!!
Answer by Alec Thilenius · Dec 20, 2012 at 05:03 AM
You are bypassing the physics by manually setting the transform's position. You need to add forces to the ridged body rather than accessing the transform component. I would go about this with a CharacterController rather than a standard collider.
To do this see: http://forum.unity3d.com/threads/95927-add-force-for-a-character-controller
One thing about character controllers, they only detect collisions if they are moving via .$$anonymous$$ove or .Simple$$anonymous$$ove for the $$anonymous$$otor component.
Your answer
Follow this Question
Related Questions
Best way for rigidbody enemy? 1 Answer
Getting a sword in place while attacking an enemy 3 Answers
My 2 Objescts doesnt collide 1 Answer
Low framerate on physics initialization. 0 Answers