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 /
avatar image
0
Question by El-Deiablo · Dec 12, 2016 at 03:05 AM · instantiaterandomprefab-instancemovetowardspoint-a-to-b

Moving instantiated prefab from randomly chosen startPoint to randomly chosen endPoint

I have been working on this for the last 4 days and after multiple attempts trying Lerp and MoveTowards I am still unable to make the instantiated prefab move from one point to the other. My issue is more complex than simply moving an object from one point to the other because the points are randomly chosen and my game object is an instantiated prefab. I am getting close, but I am having trouble figuring out what is causing this issue. Here's the issue in action - https://youtu.be/ZZ6F99TP3kc.

Here's my code:

DebrisSpawner:

 using UnityEngine;
 using System.Collections;
 
 public class DebrisSpawner : MonoBehaviour {
 
     public static DebrisSpawner instance;
     GameObject newDebris;
     public GameObject[] debris;
     public Transform[] debrisSpawnPoints;
     public Transform startPoint;
     public Transform endPoint;
     private float speed; 
     private bool spawned;
     private int level;
 
     private float spawnTime;
 
     void Start () {
     
         level = 0;
         instance = this;
     }
     
     void Update () {
 
         if (spawned == true && level == 0) {
 
             level++;
 
             float randomSize = Random.Range (0.1f, 0.4f);
             int objectIndex = Random.Range (0, debris.Length);
 
             //Debug.Log (startPoint);
 
             newDebris = Instantiate (debris [objectIndex], startPoint.position, startPoint.rotation )as GameObject;
 
             newDebris.transform.localScale = Vector3.one * randomSize;
 
             Debug.Log (newDebris.transform.position);
 
             //newDebris.transform.position = Vector3.MoveTowards (startPoint.transform.position, endPoint.transform.position, step);
 
         }
 
         if (spawned == false && level == 0) {
 
             StartCoroutine (AltDebrisSpawner ());
             int spawnPointStart = Random.Range (0, debrisSpawnPoints.Length);
             int spawnPointEnd = Random.Range (0, debrisSpawnPoints.Length);
 
             startPoint = debrisSpawnPoints [spawnPointStart];
             endPoint = debrisSpawnPoints [spawnPointEnd];
 
         }
     
     }
 
     public IEnumerator AltDebrisSpawner(){
 
         spawned = true;
 
         yield return new WaitForSeconds (4);
         level--;
         Destroy (newDebris);
         spawned = false;
 
 
     }
 
 }

DebrisMoving:

 using UnityEngine;
 using System.Collections;
 
 public class DebrisMoving : MonoBehaviour {
 
     private float speed;
 
     void Start () {
 
         speed = 0.8f;
 
     }
     
     void Update () {
 
         transform.position = Vector3.MoveTowards (DebrisSpawner.instance.startPoint.transform.position, DebrisSpawner.instance.endPoint.transform.position, 2); 
         Debug.Log (transform.position);
         
     }
         
 }

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

Answer by jmgek · Dec 12, 2016 at 06:56 PM

Is DebrisMoving attached to your prefab? If not

DebrisSpawner.cs

 newDebris = Instantiate (debris [objectIndex], startPoint.position, startPoint.rotation )as GameObject;
 newDebris.AddComponent<DebrisMoving>();

DebrisMoving.cs

  using UnityEngine;
  using System.Collections;
  
  public class DebrisMoving : MonoBehaviour {
      [SerializeField]
      float speed = 0.8f;
       
      void Update () {
         float step = speed * Time.deltaTime;
         //I don't see how your setting your endPoint position, to me you're never passing it a value. 
         transform.position =    Vector3.MoveTowards(this.gameobject.transform.position, DebrisSpawner.instance.endPoint.transform.position, step);
      }
  }

Also, I do like to stay away from any physics but possibly a rigidbody.addforce sounds perfect for you: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

Comment
Add comment · Show 4 · 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 El-Deiablo · Dec 12, 2016 at 07:59 PM 0
Share

Thanks for getting back to me! $$anonymous$$y Debris$$anonymous$$oving is attached to my prefab. Would a rigid body cause this issue (I have one attached to my prefabs and have kinematic checked)? I defined the end point on the DebrisSpawner Script on line 52 -

int spawnPointEnd = Random.Range (0, debrisSpawnPoints.Length); endPoint = debrisSpawnPoints [spawnPointEnd];

I then access this variable in the Debris$$anonymous$$oving Script:

transform.position = Vector3.$$anonymous$$oveTowards (DebrisSpawner.instance.startPoint.transform.position, DebrisSpawner.instance.endPoint.transform.position, 2);

avatar image jmgek El-Deiablo · Dec 12, 2016 at 08:42 PM 0
Share

I defined the end point on the DebrisSpawner Script on line 52 -

But where are you setting it on the Debris$$anonymous$$oving instance as far as I can tell you're only setting the end point in the DebrisSpawner?

It's a little confusing to me, why don't you let the Debris$$anonymous$$oving class handle the start and end?

     newDebris = Instantiate (debris [objectIndex], startPoint.position, startPoint.rotation )as GameObject;
     newDebris.endPos = //Put the end position here.

     public class Debris$$anonymous$$oving : $$anonymous$$onoBehaviour {
       public endPos;
      }

avatar image El-Deiablo jmgek · Dec 12, 2016 at 10:17 PM 0
Share

It's working! The only thing I would like to add is a way to make the endpoint not equal to the start point. I tried a while loop, but when I tested I received unassigned reference errors due to the transform.$$anonymous$$oveTowards being in the update and requiring the endPos. This is not absolutely necessary, but would make the setup slightly better. If you have any thoughts I would appreciate it. If not, you answered my question and I thank you for your help!

Here my final code:

Debris$$anonymous$$oving

 using UnityEngine;
 using System.Collections;
 
 public class Debris$$anonymous$$oving : $$anonymous$$onoBehaviour {
 
     private float speed;
     public Transform endPos;
     public Transform startPos;
     public static int level;
 
     // Use this for initialization
     void Start () {
 
         level = 0;
         speed = Random.Range(5,7);
     
     }
     
     // Update is called once per frame
     void Update () {
 
         Debug.Log (level);
                 
         if (DebrisSpawner.instance.spawned==true && DebrisSpawner.instance.level == 1 && level == 0) {
                 
                 int spawnPointEnd = Random.Range (0, DebrisSpawner.instance.debrisSpawnPoints.Length);
 
                 endPos = DebrisSpawner.instance.debrisSpawnPoints [spawnPointEnd];
 
                 level++;
 
         }
 
         transform.position = Vector3.$$anonymous$$oveTowards (transform.position, endPos.transform.position, speed * Time.deltaTime);
 
     }
         
 }
Show more comments

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

74 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

Related Questions

Problem with Random.range 1 Answer

How to Spawn multiple game object one by one in random order 1 Answer

Spawn GameObjects without overlap 1 Answer

Modifying/Calling Instantiated Objects 1 Answer

How to randomly spawn three non repeating gameobjects from an array? 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