- Home /
Change rigidbody object's position on collsion
So basically I want to reset a rigidbody object's position to it's original state without actually restarting the level. I have a character which can move the rigidbody object and when he falls down, (this is where the collision takes place) I want the rigidbody object to go back to it's original position. Now, the problem I am facing is that, when the collision happens, the rigidbody object does not change it's position as I have defined in the script. Instead it get's teleported somewhere else in the scene which is weird. I am very much new to scripting and it would be really nice if someone could help me out. Thank you in advance.
function OnTriggerEnter (other : Collider) {
if(other.tag == "Player"){
GameObject.Find("cuber").transform.position = Vector3(34, 0, 0);
}
}
One idea: set the Rigidbody.velocity and Rigidbody.angularVelocity to Vector3.zero at transport time. If the object was moving at the time it was transported, it will continue to move after transport.
Answer by gharbill · May 06, 2013 at 04:57 AM
The best solution to your problem is using spawn points Don't be scared! spawn points are just empty gameobjects which you can use their Transform
component to respawn your character from them. Just make some spawn points in unity editor and use them in your scrpit like this:
var mySpawnPoint: Transform; // assign a gameObject from inspector
function OnTriggerEnter (other : Collider) {
if(other.tag == "Player"){
GameObject.Find("cuber").transform.position = mySpawnPoint.position;
GameObject.Find("cuber").transform.rotation = mySpawnPoint.rotation;
}
}
Using this strategy prevents unwanted behaviours as you are facing, and also lets you deal with real position and rotation instead of Vector3 and nasty Quaternions
Your answer
Follow this Question
Related Questions
Moving object with transform.position ignore other objects even if they collided 1 Answer
Destroy object on collision or check position 2 Answers
Move object with mouse without passing through colliders 0 Answers
Collider going through walls with box collider.. 1 Answer
Rigidbody not working 0 Answers