- Home /
Store object's starting position and rotation and have it return there.
I have seen some similar questions but not with satisfactory answers to my needs.
What I want to do is store the GameObject's Transform and Rotation. When the object is knocked out of place, I was there to be a delay and then the object returns to its starting position. So far, I can get the object to return to its starting transform, but I'm struggling with the rotation.
public class putMeBack : MonoBehaviour {
private Transform myLocation;
Vector3 startPos;
bool putBack = false;
int waitTime = 2;
void Start () {
myLocation = this.transform;
startPos = myLocation.position;
}
void Update () {
if (this.transform.position != startPos && putBack == false)
{
putBack = true;
StartCoroutine(takeMeHome());
}
}
IEnumerator takeMeHome()
{
Debug.Log("take me home tonight");
yield return new WaitForSeconds(waitTime);
this.transform.position = startPos;
//this.transform.rotation = Quaternion.Euler(startPos.rotation);
putBack = false;
}
}
There's a few extra elements in there, sorry. Trying to figure it all out.
Answer by Dolzen · May 21, 2018 at 03:28 AM
Ok, you don't need to store the transform inside a variable since you can always access the transform by just typing "transform" inside the script, what you need to do is create 2 variables one for the position and one for the rotation so:
Vector3 startPosition;
Quaterion startRotation;
Then in the Start function...
Start
{
position = transform.position;
rotation = transform.rotation;
}
Then you have your normal update function, and your takeMeHome() IEnumerator should be:
IEnumerator takeMeHome()
{
Debug.Log("take me home tonight");
yield return new WaitForSeconds(waitTime);
this.transform.position = startPosition;
transform.rotation = startRotation
putBack = false;
}
As you noticed the rotation of the transform is stored in transform.rotation and its variable type is a Quaternion
I hope I've helped :)
Thank you so much Dolzen -- it worked perfectly!
Quaternions, Euler Angles, and almost anything to do with rotation still confuses me a bit. Though you answer made it look super simple ;)
I have a few questions if you have time to answer:
In the start function, I didn't have to type "this". Does Unity know that simply typing "position = transform.position" is referencing the object the script is attached to? If so, would "this" be redundant or cause problems? (I believe you touch on this in your answer, but I still don't fully understand.)
In the take$$anonymous$$eHome function, you wrote "this.transform.position" but not "this.transform.rotation". What's the reasoning there?
Thank you again for your answer! The world some how makes more sense now :D
I will be glad to answer your questions and I will answer your two questions with one shot.
When you type "this" inside the class you are saying to the compiler that you will reference any function or variable inside the same class in which you are working.
So for example if you have a class:
myClass : $$anonymous$$onobehabiour
{
//We will declare this variable called variable
Vector3 variable;
//This is a function that does not take any argumens
void $$anonymous$$yFunction1()
{
//So here you can set localVariable using either of the two ways
//You can set the variable using this.variable or just variable the two ways will give you the same result.
Vector3 localVariable = this.variable;
localVariable = variable;
}
//This is a function that takes a variable called variable as an argument
void $$anonymous$$yFunction2(Vector3 variable)
{
//Here is where using "this" will matter
//When we set localVariable to this.variable we are telling the compiler that we want to use the variable "variable" declared out of this scope
Vector3 localVariable = this.variable;
//If we use variable ins$$anonymous$$d, we are telling the compiler we want to set localVariable to the variable that is in the argument of this function
localVariable = variable;
//So here in this scope variable and this.variable are two different variables
}
}
I hope you have a better understanding of C# now have a good day!
Answer by ClaudiaKrog · Sep 05, 2020 at 09:51 PM
I am similarly moving an item, and then having the item return to its original position (I don't care about rotation). It moves backwards in reverse in order to get back to its original position. My code is about the same. However, my object never moves back 100% to its starting point, it is within.001 of it. Each time it falls, and then reverses to its original position, it gets more and more off by about .001 each time. Any ideas?
Your answer
Follow this Question
Related Questions
Rotate children (Cards) on a panel (maybe using lerp, or another method?) 1 Answer
How do I clamp the Z-Axis Rotation for this code? 1 Answer
Rotating a parent object to achieve a specific child rotation. 2 Answers
Calculate sum over all movements and rotations of object 1 Answer
How can I align axis with quaternion.fromToRotation? 1 Answer