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 /
avatar image
0
Question by will108 · Nov 22, 2012 at 10:36 PM · movinggameobjectgamobject

Need help moving object to turn around

Im learning Unity, and i want to make an object move, go a certain distance, and turn around. I can handle the turn around part. My main problem is making the object stop after moving a certain distance. This is my script so far:

 function Update ()
 {
     transform.Translate(Vector3.forward * 3 * Time.deltaTime, Space.World);
 }
 
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 Josh707 · Nov 22, 2012 at 10:57 PM

You could get it's position, start moving it with your code and keep checking it's coordinates x,y, or z values and if it equals a certain distance, break the loop.

Something like:

 public GameObject yourobjectname;
 public Transform objectPosition;
 
 public GameObject stopPoint; //You could either predefine the 
                             //stopping point with a Transform
                             //value or empty gameObject and 
                             //get its position
 void Update(){
     objectPosition = yourobjectname.Transform.position;
 
     if(objectPosition != stopPoint){ //use stopPoint.transform.position if your using the empty gameobject
         yourobjectname.transform.Translate(Vector3.forward * 3 * Time.deltaTime, Space.World);
     
     }
 
 
 
 }



I only know C# so sorry about that. I don't know how to translate it into JS but without testing this I'm gonna guess it should work with minor tweaking. Kind of inebriated and learning python so sorry if I mixed anything up, and maybe using while instead of if would be better? I don't even know. Just felt like you should get an answer.

Another idea:

 while(1){
 
     (Put moving code here)
 
     if(objectPosition == stopPoint.transform.position){
         break                        //should stop the whole thing
 
 }
 
 
 }

Don't know about this one, like I said I do not know if while(1) and break, from python, are used the same in C#. It's just an infinite loop until you break it with a keyword.

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

Destroy gameObject on collision/time? 0 Answers

rigidbody.velocity vs lerp efficiency 0 Answers

Code-moving objects dont affect Physics2D objects 0 Answers

GameObject is not reading the rigidbody2D component of instantiated prefab,GameObject is not reading the Rigidbody2D component from instantiated prefab 1 Answer

How to make an 2d object jump from A point to B point? 0 Answers


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