• 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 mcabral89 · Sep 29, 2015 at 03:10 AM · transform.translatepingpong

How to have an object move between two locations

Hey guys!

I'm having issues getting an object to move back and forth in a project I'm working on. I want to the object to move back and forth between two predefined points. I've tried using transform.translate. I can get it to move in a given direction but I cant seem to get it to move back once it reaches the desired location. I've seen some snippets and examples of using mathf.pingpong but i'm confused as how to actually us it. Could someone enlighten me please.

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 MadDevil · Sep 29, 2015 at 04:35 AM 0
Share

are the two positions between which you want the object to move fixed ?

2 Replies

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

Answer by bubzy · Sep 29, 2015 at 05:14 PM

im not great with maths, but ive just put together something that moves an object between 2 waypoints(only 2 with this code)

its messy as hell code but hopefully you might be able to learn something from it :)

 //on the object you want to move
 using UnityEngine;
 using System.Collections;
 
 public class bounce : MonoBehaviour {
 
     // Use this for initialization
 
 
     public waypoint[] waypoints;
     public string waypointOne ;
     public string waypointTwo ;
     public float speed = 0.1f;
     waypoint currentWaypoint;
     int waypointInt = 2;
     bool changeWaypoint = false;
 
     void Start () {
         GameObject[] tempWaypoints = GameObject.FindGameObjectsWithTag ("waypoint");
         waypoints = new waypoint[tempWaypoints.Length];
         int ii = 0;
         foreach (GameObject go in tempWaypoints) {
             waypoints[ii] = go.GetComponent<waypoint>();
             ii++;
         }
 
     }
     
     // Update is called once per frame
     void Update () {
         if (!changeWaypoint) {
             currentWaypoint = waypoints[0];
         } else {
             currentWaypoint = waypoints[1];
         }
         if (Vector3.Distance(transform.position,currentWaypoint.transform.position) > 1){
             transform.LookAt(currentWaypoint.transform.position);
             transform.position += transform.forward * speed;
             //you can also add some other stuff like a wait time in here
         } else {
             changeWaypoint = !changeWaypoint;
         }
 
     }
 }
 
Comment
Add comment · Show 3 · 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 mcabral89 · Sep 29, 2015 at 11:16 PM 0
Share
  I appreciate the feedback, but that's a little more than what i need.  This is what i've been trying.  thought i would use two while loops to have the object move until the Z coordinate of the object reaches a specific number then have it reverse direction.  I know that "transform.Translate (new Vector3 (0, 0, 1) x oscillationSpeed x Time.deltaTime);" works.  The way i have it set up, the object will only move on it's Z axis.  i literally only need the object to move 8 units positive then move 8 units negative.  I think part of my problem is that i'm not comparing the Z coordinate correctly.
  Also, my public variable isn't showing in my inspector anymore.  I've had it show before, but i can't get it to show back up.  I've deleted this script and worked on it a couple times so it's very likely i'm just forgetting something stupidly simple.  
  Finally, every single time i use a loop of any kind my unit locks up.  I have no idea why.  Can anyone tell me if i'm writing something wrong and causing this myself?  I'm updating my unity just to make sure it's not some kind of bug.  I'm all but certain it's my fault.
  I've decided to go a slightly different route now.  I've assigned a V3 variable with the coordinates where it begins and where it ends.  I would then like to see if current position matches a given criteria, and, if it does, change a boolean value to false.  After changing the value to false, it should kick itself out of the first loop and start the second.  With the code below i'm getting error cs0029: cannot implicitly convert type 'UnityEngine.Vector3' to 'bool'.

public class oscillation : $$anonymous$$onoBehaviour {

 Vector3 begin = new Vector3 (1.73249f, 2f, -10.5f);
 
 Vector3 end = new Vector3 (1.73249f, 2f, -2.5f);    
 
 public float oscillationSpeed;
 
 private bool change = true;
 
 // Use this for initialization
 void Start () {
     
     
 }
 
 // Update is called once per frame
 void Update () {
     while(change = true) {
         transform.Translate (new Vector3 (0, 0, 1) * oscillationSpeed * Time.deltaTime);
         if(transform.position = end)
             change = false;
     }
     
     while (change = false) {
         transform.Translate (new Vector3 (0, 0, -1) * oscillationSpeed * Time.deltaTime);
         if(transform.position = begin)
             change = true;
         
     }
     
     
 }

}

avatar image bubzy mcabral89 · Sep 30, 2015 at 04:51 PM 0
Share

ok :)

try this then

 using UnityEngine;
 using System.Collections;
 
 public class simpleBounce : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     public float unitsTo$$anonymous$$ove = 8;
     Vector3 startPos;
     bool towards = true;
     public float speed = 0.1f;
     void Start () {
         startPos = transform.position;
     }
     
     // Update is called once per frame
     void Update () {
     if (towards) {
             transform.position += new Vector3 (0, 0, speed);//whatever you want to modify the speed with
             if (transform.position.z > startPos.z + unitsTo$$anonymous$$ove) {
                 towards = false;
             }
         } 
     else {
             transform.position -= new Vector3 (0, 0, speed);//whatever you want to modify the speed with
             if (transform.position.z < startPos.z - unitsTo$$anonymous$$ove) {
                 towards = true;
             }
         }
     }
 }
 

there are no loops in this to get stuck in , and you can change the value in the inspector of the units you want to move :)

hope this helps

avatar image mcabral89 · Sep 30, 2015 at 10:45 PM 0
Share

Thank you sir! After a little tweaking, I got it working with your example. Ironically, i found another post shortly after explaining how to use PingPong. I'm trying to decide which way i prefer.

avatar image
0

Answer by mcabral89 · Sep 29, 2015 at 12:19 PM

Yes they are. I've seen examples that will bounce back and forth based on time (will go one direction for x seconds then go the other direction for x seconds). I've also seen examples of using SIN. I don't know if this will help me. i have three objects total that i am using and two of those will move exclusively on the z axis. It has been a while since i've done anything with SIN even on a purely mathematical level much less using it in code. I'm really just looking for the most efficient way to pull off this movement. I don't want all kinds of useless code floating around if it can be avoided.

I've also been having problems with infinite loops. I've been experimenting with a couple different loops to help facilitate the movement because i need it to keep moving autonomously for the duration of the program. However, my unity seems to lock up every time i add an infinite loop. Any ideas on this?

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

30 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

Related Questions

transform.Translate how to stop the movement 1 Answer

How can I move an object between two positions, while it is on a rotating platform? 0 Answers

Limit movement to just forward/backward and left/right 0 Answers

how to rotate a gameobject between two angles front and back direction? 0 Answers

Set limit to transform.translate 2 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