Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ZachJohnson · May 21, 2018 at 03:11 AM · quaterniontransform.rotation

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZachJohnson · May 22, 2018 at 02:09 AM 0
Share

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:

  1. 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.)

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

avatar image Dolzen · May 22, 2018 at 02:52 AM 1
Share

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!

avatar image ZachJohnson Dolzen · May 23, 2018 at 12:53 AM 0
Share

Thank you so much!

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

86 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges