- Home /
AI GameObjects Go 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 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 clunk47 · Dec 20, 2012 at 02:52 AM
Set your tiger's rigidbody Collision Detection mode to Continous Dynamic in the inspector, or via script like so:
rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
This is so that fast moving objects will ALWAYS check for collisions, unlike the "Discrete" option.
If this doesn't help, you could also give the walls a Rigidbody component and set their collision detection mode to Contiuous (Not Dynamic). You would need to constrain the walls so they can't be moved, but you DO NOT want to do this by setting them as Kinematic. There is a constraints option in the inspector, lock rotation and movement on all axes, or do this via script like so:
rigidbody.constraints = RigidbodyConstraints.FreezeAll;
Another approach I can think of, instead of moving the tigers with transform.position, try using rigidbody.velocity in the same manner like:
rigidbody.velocity = transform.forward * moveSpeed * Time.deltaTime;
This should help, if not I can brainstorm some more :)
Thanks so much for your response! I think the last tip worked (the one with rigidbody velocity ins$$anonymous$$d of transform position) except the tiger didn't move. So I use rigidbody velocity and transform position to locomote the tiger and it works! Thanks a lot!!!
Happy I could help! I voted up your question for it being a good one :) If this seems to be a resolution for your issue, I would be greatly appreciative if you could Vote up (thumbs up icon) and Accept the answer (Check$$anonymous$$ark). Have a great day! :D
Well you don't have to accept the answer if it doesn't completely resolve the problem :) That way people will know you still need help, sorry I was unable to resolve.
Try the velocity method again, and turn UP the movespeed, see if that helps. If not, use some Constant Force. Then in script use constantForce.force = transform.forward moveSpeed Time.deltaTime;
constantForce.force is a Vector3 so it won't be too hard to experiment w/ :)
Your answer
Follow this Question
Related Questions
Gun goes through wall in a FPS 5 Answers
Collision Problem with Rigidbody? 1 Answer
OnTrigger Box Collider Doesn't Work? 1 Answer
A node in a childnode? 1 Answer
Object Collider problem, objects going through walls? 1 Answer