Reset object position after reaching certain point

I’ve making a game where you need to roll a ball in round maze to the center. When it reachs the center ( 0,0,0 point ), i need the ball to appear at the start again.

Based on

I’ve come to something like that:

Blockquote
public class Resetposition : MonoBehaviour {

static Vector3(0,0,0) = zero;
private Vector3 initialPosition;
private Quaternion initialRotation;

void Start () {
	initialPosition = transform.position;
	initialRotation = transform.rotation;
}

void Update () {
	transform.Translate(Vector3.up * Time.deltaTime);
	
	if( gameObject.transform.position = zero)
	{
		transform.rotation = initialRotation;
		transform.position = initialPosition;
	}
	
}

}

But i am not sure about how to change it further to make it correct. As i understand, i’ve stored initial position of my object, and when it reachs 0,0,0 it should appear at starter position again - with correct changes.

Two points:

  1. As WhoRainZone1 pointed out - the definition of static Vector3(0,0,0) = zero won’t compile.
  2. Getting a precise match to a Vector3 on 0,0,0 is unlikely - you’d be better to check the distance between your transform and 0,0,0 according to some arbitrary value:

e.g.

private Vector3 initialPosition;
private Quaternion initialRotation;
private float distance = 0.5f;  // change this depending on how "close" it needs to be
 
void Start () {
    initialPosition = transform.position;
    initialRotation = transform.rotation;
}
 
void Update () {
    // Move the object upwards??
    transform.Translate(Vector3.up * Time.deltaTime);
 
    // Reset the position if we are sufficiently close to (0,0,0)
    if( Vector3.Distance(transform.position, Vector3.zero) < distance)
    {
        transform.rotation = initialRotation;
        transform.position = initialPosition;
    }
 
}

Hope this helps!

0,0,0 Seems really exact. I could be wrong, but you may find it more difficult than you expected to get the object to exactly the center. That being said, I’m not sure how to haven’t received an error with static Vector3(0,0,0) = zero;. Maybe I’m missing something but that statement is syntactically very wrong.

static Vector3 zero = new Vector3(0,0,0);

That is how you’d create a vector at 0,0,0.

I would however recommend using a collider mesh as a trigger, make it very small, and place it at 0,0,0. Just create a cube, hide the mesh renderer, and tick the isTrigger box in the inspector under Mesh Collider. Then in a script attached to the collider write:

void OnTriggerEnter(Collider go){
    // Add some checks to make sure the go (gameobject) is the ball
    go.transform.position = initialPosition;
    go.transform.rotation = initialRotation;
}

This tactic will give you more control over where the “zero” is and how easy it is to get to it. You could even copy/paste the object and throw it all over your scene, and with no other effort have a bunch of areas that will reset the ball. Hope that helps. :slight_smile: