So I want my sphere to collide with a cube, then, after colliding with it, move to another cube, but nothing is happening. here’s my script.
var start : Transform;
var end : Transform;
var end2 : Transform;
function Update () {
transform.position = Vector3.Lerp (transform.position,end.position,Time.time);
}
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.name == "hit")
transform.position = Vector3.Lerp (transform.position, end2.position,Time.time);
if(collision.gameObject.name == "hit")
Debug.Log("Game over!");
}
as you can see I also have a collision script for a Game over debug log, which does nothing as well. I have no error messages so…?
sangi93
November 21, 2010, 3:49pm
2
You may check if your cube is called hit. Also, your sphere actually moves, but only for that frame when sphere collides with cube.
Untested code, but should work:
var start : Transform;
var end : Transform;
var end2 : Transform;
var isGoingBack : boolean = false;
function Update () {
if(!isGoingBack)
transform.position = Vector3.Lerp (transform.position,end.position,Time.time);
else
transform.position = Vector3.Lerp (transform.position, end2.position,Time.time);
}
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.name == "hit")
isGoingBack = true;
if(collision.gameObject.name == "hit")
Debug.Log("Game over!");
}
sangi93
November 21, 2010, 3:58pm
3
To register OnCollision events one of your colliders must have rigidbody or character controller attached
Thanks for the input first off.
I copied that code exactly, I still get no error messages, and still get the same result. I did notice if I check the “isGoingBack” check box while the game is running it pops to end2 position, so there must be some reason it’s not registering the collision.
and at the moment both the objects have a rigidbody attached.
sangi93
November 22, 2010, 8:33am
5
I’ve tested my code and it worked. Test scene is attached. If I did something wrong please write.
435103–15128–$ballsTest.unitypackage (9.97 KB)