How do I get an object back to its initial position in Unity?

Asked

Viewed 2,105 times

0

I have an object that is hit by the raycast and use cObj.transform.position = Vector3.MoveTowards(cObj.transform.position,main.transform.position, step); for the object to go against the camera.

How do I get him back to his starting position? I have a button that when focused the image goes back to where it came from.

  • That object that is hit by a raycast the initial position of it is always the same ?

3 answers

1

Look I would store the starting position in a variable and when hit I would load the transform position setting the values that were set in the variable.

Maybe there’s a more practical way, but I’d do it this way

  • I tried it, but it doesn’t go back into position before moving forward

  • It can show more code, to evaluate better the problem?

1

I would create a variable with the initial position between the camera and the object you want to move and then to return to the original position would:

var posicao_inicial =  new Vector3(3,3,3);


cObj.transform.position = Vector3.MoveTowards(cObj.transform.position, main.transform.position-Vector3.Distance(cObject.position, posicao_inicial), step);

I haven’t worked with Unity for a while, and I don’t have any way to test it here, but I think something like that will work.

0

Julia, create an ex variable. Vector3 posicaoInicial, to store the position, then in the Start method put startNitial = Transform.position;

Then when you want it to go back to its initial position you use: Transform.position = starting position;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.