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 /
  • 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.

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

Preventing A Teleporting GameObject From Passing Through Walls 2 Answers

Textures, Materials, Meshs and Maps? 1 Answer

Slerp Rotating 1 Degree and Limiting All Other Rotation 1 Answer

Convert 2d object in 3d 1 Answer

Removing runtime array exception (C#) 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