- Home /
How can I reset a rigidbody?
I'm trying to make a game where you need to make it through a maze. If you hit an object, you start flying into space, and are forced to reset. I have a script that resets your position, but when I reset the position, I am still flying into space. How can I add some code to my reset script so that the position is reset, AND the flying and spinning is reset (I believe this is the rigidbody)
Here is the reset script
using UnityEngine;
using System.Collections;
public class BasicPlayerReset : MonoBehaviour {
private Vector3 newPos;
void Awake(){
newPos = transform.position;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
ChangePosition();
}
void ChangePosition(){
Vector3 PlayerSpawn = new Vector3(0f , .9f , 0f);
if(Input.GetKey(KeyCode.V)){
newPos = PlayerSpawn;
transform.position = newPos;
}
}
}
Answer by Linus · Nov 15, 2014 at 12:27 AM
I think resetting velocity should do the trick
if(Input.GetKey(KeyCode.V)){
newPos = PlayerSpawn;
rigidbody.velocity = new Vector3(0f,0f,0f);
rigidbody.angularVelocity = new Vector3(0f,0f,0f);
transform.Rotation = Quaternion.Euler(new Vector3(0f,0f,0f);
transform.position = newPos;
}
Thanks, but this doesn't solve the spinning, when I bump into an object, the rotation of the X Y and Z are crazy, but it does reset the movement, thanks. Any idea how to reset the rotation? I will do some experimenting, but if you could answer. Thanks.
I did this, and now the rotation stays at 0, 0, 0 if i HOLD DOWN, "V"
using UnityEngine; using System.Collections;
public class BasicPlayerReset : $$anonymous$$onoBehaviour {
private Vector3 newPos;
private Quaternion newRot;
void Awake(){
newPos = transform.position;
newRot = transform.rotation;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
ChangePosition();
}
void ChangePosition(){
Vector3 PlayerSpawn = new Vector3(0f , .9f , 0f);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.V)){
newPos = PlayerSpawn;
rigidbody.velocity = new Vector3(0f,0f,0f); //Vector3.zero would be same thing but dont recall if you should use new keyword in that case
transform.position = newPos;
transform.rotation = newRot;
}
}
}
I had forgot about that, that would be http://docs.unity3d.com/ScriptReference/Rigidbody-angularVelocity.html
Updating answer with stopping rotation and resseting it to 000
THAN$$anonymous$$ YOU SO $$anonymous$$UCH, WHEN I FINISH $$anonymous$$Y FIRST GA$$anonymous$$E EVER, I A$$anonymous$$ GOING TO WATCH $$anonymous$$ORE VIDEOS, SORRY FOR ALL CAPS, THAN$$anonymous$$ YOU SO $$anonymous$$UCH!
Glad it worked out. Best way to say thanks is to select answer, so the question gets closed and we both get points. For you that means you get out of moderation que. Your question has been answered many times before but I understand that it can be hard to know what to search for when new to Unity
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to double jump 1 Answer
C# Make something happen for X amount of Seconds. 2 Answers
A quick question C# 1 Answer