Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by Kenneth Andersen · Dec 17, 2011 at 04:19 PM · c#positionobject

Check if an object is changing position C#

Hello, how do I check if an object is changing position or not. I wan't to know this because I want to be able to do something, when an objects x position is getting greater, and I want to do something else when it's getting smaller.

Comment
Add comment · Show 1
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 talhahasan · Jul 07, 2015 at 12:15 PM 0
Share

I was looking for code that can detect change in position repeatedly , like every update, this code will work only once the object moves.

3 Replies

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

Answer by aldonaletto · Dec 17, 2011 at 05:25 PM

To monitor only the X coordinate of the object's position, you could do this:

 Vector3 lastPos;
 Transform obj; // drag the object to monitor here
 float threshold = 0.0f; // minimum displacement to recognize a 
 
 void Start(){
   lastPos = obj.position;
 }
 
 void Update(){
   Vector3 offset = obj.position - lastPos;
   if (offset.x > threshold){
     lastPos = obj.position; // update lastPos
     // code to execute when X is getting bigger
   }
   else
   if (offset.x < -threshold){
     lastPos = obj.position; // update lastPos
     // code to execute when X is getting smaller 
   }
 }

This will execute the code once when the X distance from the lastPos exceeds threshold. This also updates lastPos when the code is executed, thus the distance from this point must again exceed threshold for the code to be executed again.
This script is intended to be attached to another object; if you plan to use it in the monitored object, replace obj. in the text by transform. (or assign obj = transform; at Start).

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
avatar image
4

Answer by TWObitEDD · Dec 01, 2014 at 08:23 PM

Thank you for your answer Aldonaletto, I broke it down to read it so I figured I would re-post for future readers (see Below).

But I also wanted to point everyone to Transform.HasChange(); Which can be used similarly.

     Vector3 lastPos; 
     
     Transform obj; // drag the object to monitor here 
     
     float threshold = 0.0f; // minimum displacement to recognize.
     
     void Start(){ lastPos = obj.position; }
     
     void Update()
     { 
         Vector3 offset = obj.position - lastPos; 
     if (offset.x > threshold){ lastPos = obj.position; 
     // update lastPos 
     // code to execute when X is getting bigger 
     } 
     else if (offset.x < -threshold)
     { 
     lastPos = obj.position; 
     // update lastPos 
     // code to execute when X is getting smaller 
     } 
     } 
     /*This will execute the code once when the X distance from the lastPos exceeds threshold. 
     This also updates lastPos when the code is executed, thus the distance from this point must 
     again exceed threshold for the code to be executed again. 
     This script is intended to be attached to another object; 
     if you plan to use it in the monitored object, replace obj. in the text by transform. (or assign obj = transform; at Start).*/
 


Comment
Add comment · Show 1 · 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 FlipLnn · Oct 20, 2017 at 12:02 PM 0
Share

Transform.HasChanged() was the solution for me, thanks!

avatar image
3

Answer by YasinJavaid_ · Sep 24, 2021 at 07:06 AM

3d object or all axis changing position with define threshold.

 public float thresholdChange = 0.05f;
 private Vector3 lastTransformPos;
 private void Start()
     {
         lastTransformPos = transform.position;
     }
  
 private void Update()
     {
         var diffVector = transform.position - lastTransformPos;
         if (diffVector.magnitude >= thresholdChange)
         {
             lastTransformPos = transform.position;
            //Do something 
         }
     }
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

10 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

Related Questions

Distribute terrain in zones 3 Answers

Returning position of game object in c# 2 Answers

Multiple Cars not working 1 Answer

How do i have multiple objects to rely on a objects position? 1 Answer

moving random objects to random positions 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