Hi!
I got a quick n00b question about this standard script:
var explosion : Transform;
// When a collision happens destroy ourselves
// and spawn an explosion prefab instead
function OnCollisionEnter () {
Destroy (gameObject);
var theClonedExplosion : Transform;
theClonedExplosion = Instantiate(explosion,
transform.position, transform.rotation);
}
How do I correctly check for the speed of the (player controlled) object that hits the object attached to this script, and how do I make so it only runs this function if that speed is greater then a variable?
Thanks in advance!
Thomas
You use
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.moveLength > .3){
//explosion code
}
}
The moveLength is the speed of the object. You’ll have to adjust the number.
Thanks!
But unfortunately, it doesn’t work… Does hit.movelengt checks the speed of the object to wich the script is attached? (They don’t move, they’re just crates on the road)…
Any other suggestions?
NO, it checks the speed of the object that collided. What you should do for starters is put:
function OnControllerColliderHit(hit : ControllerColliderHit) {
Debug.Log(hit.moveLength);
}
Then see what numbers it gives you. Drive slow and hit it, then drive fast and hit it. See if the numbers are different.
It doesn’t show any number at all… Nor any string for that matter… It looks like function OnControllerColliderHit(hit : ControllerColliderHit) doesn’t occur… ?
This is how the whole script should look like, right?
OnControllerColliderHit is only called if your player character has a Character Controller component. And if I remember correctly, it is only called on the scripts attached to the player, not on the scripts attached to what hits the player. Does your player character have a Character Controller attached? If not, are you using rigidbodies?
Edit:
It looks like what you might want to do is almost identical to what is on the OnCollisionEnter page of the docs. The example plays a sound if they are moving fast enough, so you could use the same code to detect how fast your collision is and then destroy the object if you want.
No, my player charachter does not have a Character Controller attached to it… It is a modified Car from the Car Tutorial… It is a rigidbody though, and so are the objects I wanna hit…
The main problem is with the script I have, is that the objects are destroyed as soon as I hit play, due to collission with the terrain etc…
Preferably the script should check 2 things: 1-what it’s hit by, and if its the Car gameobject: 2 Is that speed greater than X?
The worst part is I’ve maid this script already, when I first started using Unity, about 2 months ago… Since then I’ve focused on design, but unfortunately, a OS re-install made me lose my first project containing al those scripts…
Hmm. OK, it doesn’t work for a collider, only a character controller.
try this:
function OnCollisionEnter( collision : Collision) {
if (collision.relativeVelocity.magnitude > 2){
//explosion code
}
}
That works, but in the same way as my starter script; instant destroy Instantiate as soon as the objects touch the terrain.
OK well, get the numbers
function OnCollisionEnter( collision : Collision) {
Debug.Log(collision.relativeVelocity.magnitude );
}
Also, put it as close to the terrain as possible without touching. Then just set the number above what it first reads.
Yes, that’s it! Thanks man!!
Just out of curiosity; How do I check What is hitting the object?
You just use
var go = collision.gameObject;
Normally you would check for a tag like:
If (go.tag == “player”){
do something();
}
or you could check for the name or whatever.