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 Qasim97 · Feb 15, 2017 at 04:23 PM · movementscripting beginnerhow to

Move an object move from point a to point b to point c to point d and back to point a?

I need an object, specifically a spawn point to slowly move from point a,b,c, and d, back to point a continuously throughout the game. This is the code I have so far and beware it is longer than I think it ought to be. I really need help because I cannot find a way to do this. I am very new to this and don't know the terms for a lot of functions, methods and variables. If anyone can help me I would be grateful. I am however going to still think of ways to do this. Also just in case you were wondering, I am purposefully trying to find ways out by myself to make myself better at this. Thanks in advance!

The problem that happens with this specific code is that the object only moves 0.2 places in the first direction and then stops. Other problems I have had with different ways of doing this is the object going straight into the centre of the 4 places i want it to go.

 public class Spawn_Point_Movement : MonoBehaviour {
 
     public Vector3[] currentPoint = new Vector3[4];
     public float speed;
     void Update () {
         float step = speed * Time.deltaTime;
 
         if (transform.position == currentPoint [0] && transform.position != currentPoint [1] 
             && transform.position != currentPoint [2] && transform.position != currentPoint [3]
             || transform.position == Vector3.MoveTowards (transform.position, currentPoint [1], step) 
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [0], step)    
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [2], step)
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [3], step)) {
             transform.position = Vector3.MoveTowards (transform.position, currentPoint [1], step); 
         }
 
         if (transform.position == currentPoint [1] && transform.position != currentPoint [2]
             && transform.position != currentPoint [0] && transform.position != currentPoint [3]
             || transform.position == Vector3.MoveTowards (transform.position, currentPoint [2], step) 
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [0], step) 
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [1], step)
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [3], step)) {
             transform.position = Vector3.MoveTowards (transform.position, currentPoint [2], step); 
         }
 
         if (transform.position == currentPoint [2] && transform.position != currentPoint [3]
             && transform.position != currentPoint [1] && transform.position != currentPoint [0]
             || transform.position == Vector3.MoveTowards (transform.position, currentPoint [3], step) 
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [0], step) 
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [2], step)
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [1], step)) {
             transform.position = Vector3.MoveTowards (transform.position, currentPoint [3], step); 
         }
 
         if (transform.position == currentPoint [3] && transform.position != currentPoint [0]
             && transform.position != currentPoint [2] && transform.position != currentPoint [1]
             || transform.position == Vector3.MoveTowards (transform.position, currentPoint [0], step) 
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [1], step) 
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [2], step)
             && transform.position != Vector3.MoveTowards (transform.position, currentPoint [3], step)) {
             transform.position = Vector3.MoveTowards (transform.position, currentPoint [0], step); 
         }
             
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Feb 16, 2017 at 01:12 AM

hex posted while i was typing. thought i would still answer. just name a bunch of game objects as "marker1", "marker2","marker3" and so on. and put this script on your object you wanna move. this way you can have more than 4 if you wish.

     public int target;
     public Vector3[] markers;
 
 
     bool b;
     int i;
     void Start () {
 
         //Put however many Empty game objects in your scene for position markers and name them "marker1" and "marker2" and "marker3"
 
         b = false;
         i = 0;
         while (!b) {if (GameObject.Find ("marker" + i)) {i++;} else {b = true;}} 
         markers = new Vector3[i];
         while (i>0) {i--;markers[i] = GameObject.Find ("marker"+i).transform.position;
 
         }    
         
     }
     
     
     void Update () {
         transform.position = Vector3.MoveTowards (transform.position,markers[target], 2f);
         if (transform.position == markers[target]) {target++;
             if(target==markers.Length){target=0;}
         }
         
     }

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
0

Answer by hexagonius · Feb 15, 2017 at 05:16 PM

 public Vector3[] currentPoint = new Vector3[4];
 int targetIndex = 0;
      public float speed;
      void Update () {
 transform.position = Vector3.MoveTowards(transform.position, currentPoint[targetIndex], Time.deltaTime * speed);
 if (transform.position == currentPoint[targetIndex])
 targetIndex = (targetIndex + 1) % currentPoint.Length;
 }
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 Qasim97 · Feb 15, 2017 at 05:40 PM 0
Share

Thank you so much! This worked perfectly and I learnt something new! :)

avatar image Narden · Apr 23, 2017 at 08:33 PM 0
Share

can you explain more please :) .. the vector have what?

avatar image Narden · Apr 23, 2017 at 08:35 PM 0
Share

the marker object which is currentPoint1, currrentPoint2 and so on??

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

116 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 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 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

Help me to do this moving player script 0 Answers

Script problems c# 0 Answers

Rigidbody2D movement is lagging 0 Answers

How do I make my movement script move the character UNTIL I let go of the key? 1 Answer

Restarting after pressing a key 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