• 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 /
  • Help Room /
avatar image
0
Question by meechan007 · Jan 23, 2016 at 02:39 PM · transform3dbegginer

How to move a object from point A to point B and back again (3D)

I am trying to move a object across the screen and have written a script that can move it from its spawn to the place i want it to go but i can't get it to go back. I know that there is a problem with the script as i used if statements as i did not know a better way of moving it. The object starts at X 12 and should stop at X 2.5 and return to the spawn point however i cant get it back.

using UnityEngine; using System.Collections;

public class Mover : MonoBehaviour { void Update() {

     if (transform.position.x < 2.5) {
         transform.Translate (Vector3.right * Time.deltaTime);
     } 
     else  
     {
         transform.Translate(Vector3.left * Time.deltaTime);
     }

     //transform.Translate(Vector3.up * Time.deltaTime, Space.World);
 }

} Any and all help is appreciated.

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
1
Best Answer

Answer by DCordoba · Jan 23, 2016 at 05:57 PM

first, maybe you seeing it from a rare point of view, this script just will move the object until less than 2.5, but when reaches this position will back again just one frame because next frame the object will be a bit more than 2.5, so will came down to less 2.5 again.

so, the object go well until reaches 2.5, after that just jump between two positions transform.x <2.5 and transform.x >= 2.5

so you need to simulate a state of the object, and change it when reach 2.5

first declare a Enum (or something) out of the current method to simulate the state, and use that state to check if the object have reached yet the point of 2.5

some like

 public enum GoingTo
 {
     Forward, Backward, None
 }
 public class Mover : MonoBehaviour { 
 void Update() {
     GoingTo State = GoingTo.Forward; 
     if (transform.position.x < 2.5f) {
         //if you have reached the maximium position
         //En español, si ya has llegado a la maxima posicion
         State = GoingTo.Backward;
     } 
     else  
     {
         //if you are going backward and reached the original position
          //si estas regresando y alcanzaste la posicion original
         if(State == GoingTo.Backward && 12.0f <= transform.position.x){
             State = GoingTo.None;
         }
     }
     //here make movement of each "state"
     //aqui se "da movimiento" a cada "estado"
     switch(State){
     case(GoingTo.Forward):
         transform.Translate (Vector3.right * Time.deltaTime);
         break;
     case(GoingTo.Backward):
         transform.Translate(Vector3.left * Time.deltaTime);
         break;
     default:
         //you have rached the original position, what you'll do now?
         //has alcanzado la posicion original, ¿ahora que sigue? (borra este print, se ejecuta cada frame y te sobrecargará el procesador)
         Debug.Log("Movement Completed, Waiting...  Completado el movimiento, Esperando...");
         break;
     }
 }
 }







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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

41 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

Related Questions

Transform.position of object not the same as shown on the inspector 1 Answer

Bug floating object when fixed to bones 0 Answers

Unity Lerp does not work correctly when gameObject rotation changes 0 Answers

How do you maintain gravity using transform.forward and transform.right? 0 Answers

Ello~ I need help with transform.parent = null; When I play it and press the DropWeapon input button it goes in a random location and doesnt move . 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