Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Kai_Gil · May 16, 2013 at 02:35 AM · positionmovingfind

Finding one of the several positions of a moving GameObject

Hi again peeps. Quick question: How do I find and record into a floating variable the position of a moving object?

Here's my JS code:

 var gel : Transform;
 var on = false; 
 var speed : float = -20;
 var chDir = false;
 var release = false;
 var start = true;
 
 function OnTriggerEnter (pipCollider) 
 {
     if(pipCollider.gameObject.name == "Pip")
     {
         on = true;
     }
 }
 
 function Update() 
 {
         if(start)
         {
             gel.transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
         }
         
         if(on)
         {
             start = false;
             var pivot = pip.position;
             gel.transform.RotateAround (pivot, Vector3.forward, 50 * Time.deltaTime);
             chDir = true;
         }
         else if (!on){}
         if(chDir && Input.GetButtonDown("Fire1"))
         {
             on = false;
             release = true;
         }
         if(release)
         {
             var pivotor = pip.position;
             var orbitor = gel.position; //here lies the problem - we want a snapshot of the gel position not the uptodate one
             var vect = (orbitor-pivotor).normalized;
             var newSpeed = speed*vect;
             gel.transform.Translate(newSpeed * Time.deltaTime);
         }
 }


In the if(release) statement, the orbitor variable is set to the position of the gel Transform object. However, this variable changes over time and I want the orbitor to remain a fixed snapshot of the gel position at the moment the mouse is clicked.

For clarity: I want to put snapshot values into the orbitor variable that do not change after the object has moved. I also want to be able to swap that value for another snapshot later on.

Any thoughts?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · May 16, 2013 at 02:43 AM

You are doing this right here since you are storing the position in a Vector3. Vector3s are structs which get a copy of data. If you were saving the transform, then you would be saving a reference to the data, which would then change every frame as transform.position changed.

The reason your value is changing every frame is that you set release to 'true' so your code block on lines 38 - 42 will get executed every frame. One way to fix it is to move it into the block above. Input.GetButtonDown() only returns true for a single frame, so if you move lines 39 and 40 inside of the 'if' block on lines (lines 32 - 35), the positions will only be saved once. The second problem is scoping. You are declaring the variable inside of and 'if' statement inside of Update(), so it gets reset every Update() call. So assuming orbitor is the position you want to save, the code would look like:

At the top of the file:

 var orbitor : Vector3;

and then inside Update:

    if(chDir && Input.GetButtonDown("Fire1"))
    {
      on = false;
      release = true;
      orbitor = gel.position; 
    }

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

13 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

Related Questions

How do I find the position of an element in an array? 3 Answers

Find position of a character of typogenic text 1 Answer

Moving parent to position of child 0 Answers

Move object A towards object B 2 Answers

Random position between 4 vectors 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