Ok, first of all, I like to try to figure things out instead of asking what is probably a really noob question, but I have to admit that this is kicking my butt. I have some vehicles that are instatiated at one end of a tunnel and then they should be destroyed when they reach a collider on the other end. This seems like a simple application and theoretically shouldn’t cause any problems, right?
Well, the vehicles just go right through the collider and continue on into infinity.
This is the code that I am using (copied directly from the scripting reference manual):
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject);
}
I have tried:
Mesh colliders on the vehicles
box colliders on the vehicles (is trigger and isn’t trigger)
rigid body on the vehicle
Mesh colliders on the target
box collider on the target (is trigger and isn’t trigger)
rigid body on the target
I have tried all of the colliders and every combination of colliders, triggers, is kinematic, etc. that I could come up with and still the vehicles just sail right through. Help!
I have the “Destroy” script on a box collider at the exit of the tunnel. I have tried both adding a rigid body to this object and/or adding it to the vehicles.
I have checked the “is trigger” box on one, the other, and then both.
If my character collider hits the end of the tunnel, it destroys the character.
So you are saying maybe my vehicles are moving too fast?
BoxCollider, isTrigger and kinematic rigidbody on the target trigger object
Any collider (ideally not mesh) on the vehicle with isTrigger set to false, plus a non-kinematic rigidbody.
Also, are you using the rigidbody force methods (eg, AddForce) to move the vehicle, or just setting its position using the transform? Generally, it is more reliable to move a physics object using forces rather than set its position - it is quite easy to move an object straight from one side of a thin collider to the other without the physics system having any chance to detect a collision.
I was just using the transform. With addForce, I get vehicles tumbling wildly down the road.
I am using:
var speed = 10.0;
function FixedUpdate () {
rigidbody.AddForce (Vector3.forward * speed);
}
However, now they disappear if they tumble into the end of the tunnel so there is some progress. How do I use AddForce so that the force is constant and contrained to one direction?
Ok, so now I rebuilt my vehicle model so that each tire is its own object and then added wheel colliders. The problem is that the wheel colliders are oriented incorrectly - they are sideways. Now the car will roll sideways nicely, but will not roll forwards. How do you rotate just the wheel collider so that it is facing the same direction as the wheel object?
The Z direction (blue axis) of the WheelCollider object is the forward direction. It’s best not to attach the WheelCollider to the graphical wheel object - make separate child objects for the colliders.
Ok, that worked! To make it easier, I duplicated each wheel, made them children of the original wheel, rotated them by 90 degrees, added the wheel collider, and then deleted the wheel mesh leaving just the collider.
Just for anyone else trying to do something similar - check the “freeze rotation” too to make sure that your player can’t push the vehicle over if they hit it from the side.
Now I just need to add wheel animation and figure out a script to make the player dead if hit and I will be all set.