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;
		
		}
		
	}
}

Please format your code correctly by highlighting it all and pushing the button labeled 101/010

1 Answer

1

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 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

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

I'm new to programming in general, i clicked the thumbs up, and said i need a reputation of 15, so i clicked the green checkmark. Thank you very much again xD. Making my first game now :).