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 /
This question was closed Dec 12, 2016 at 02:35 AM by El-Deiablo for the following reason:

Other

avatar image
0
Question by El-Deiablo · Dec 07, 2016 at 03:20 AM · movingprefab-instancerandomizepoint-a-to-b

Moving an instantiated prefab from random point A to random point B

I have a prefab that is instantiated at a randomly chosen point (spawnPointStart) which is my Point A and another point is chosen (spawnPointEnd) which is Point B. I have tried using Lerp and MoveTowards along with invoke repeating and coroutines. When I run the game the object is instantiated. Sometimes the object stays still and other times the object will jump to random spots. I also looked into making a game object move in random directions. I want debris to appear and move randomly. I was hoping someone may have some suggestions.

Here's what I got (updated code with new attempts):

Debris Spawner

 using UnityEngine;
 using System.Collections;
 
 public class DebrisSpawner : MonoBehaviour {
 
     public static DebrisSpawner instance;
     public GameObject[] debris;
     public Transform[] debrisSpawnPoints;
     private float speed; 
     private bool spawned;
     //public GameObject debris;
     //public Transform startSpawnPoint;
 
     private float spawnTime;
 
     // Use this for initialization
     void Start () {
     
         instance = this;
         //spawnTime = Random.Range (5, 10);
         //InvokeRepeating ("SpawnDebris", spawnTime, spawnTime);
 
     }
     
     // Update is called once per frame
     void Update () {
 
         /*if (RocketOrbit.buttonDown == true) {
 
             CancelInvoke ("SpawnDebris");
         }*/
 
         if (spawned == false) {
 
             StartCoroutine (AltDebrisSpawner ());
 
         }
     
     }
 
     /*void SpawnDebris(){
 
         speed = Random.Range (0.1f, 1.0f);
 
         float randomSize = Random.Range (0.1f, 0.4f);
         int spawnPointStart = Random.Range (0, debrisSpawnPoints.Length);
         int spawnPointEnd = Random.Range (0, debrisSpawnPoints.Length);
         int objectIndex = Random.Range (0, debris.Length);
 
         GameObject newDebris = Instantiate (debris [objectIndex], debrisSpawnPoints [spawnPointStart].position, debrisSpawnPoints [spawnPointStart].rotation )as GameObject;
 
         newDebris.transform.localScale = Vector3.one * randomSize;
 
         
             Vector3 newPos = Vector3.MoveTowards (newDebris.transform.position, debrisSpawnPoints [spawnPointEnd].transform.position, speed);
             newDebris.transform.position = newPos;
 
         //newDebris.transform.position = Vector3.MoveTowards (newDebris.transform.position,
             //debrisSpawnPoints [spawnPointEnd].transform.position, speed);
 
     }*/
 
     public IEnumerator AltDebrisSpawner(){
 
         spawned = true;
 
         speed = Random.Range (0.1f, 1.0f);
 
         float randomSize = Random.Range (0.1f, 0.4f);
         int spawnPointStart = Random.Range (0, debrisSpawnPoints.Length);
         int spawnPointEnd = Random.Range (0, debrisSpawnPoints.Length);
         int objectIndex = Random.Range (0, debris.Length);
 
         GameObject newDebris = Instantiate (debris [objectIndex], debrisSpawnPoints [spawnPointStart].position, debrisSpawnPoints [spawnPointStart].rotation )as GameObject;
 
         Vector3 targetTravel = new Vector3 (debrisSpawnPoints [spawnPointEnd].transform.position.x, debrisSpawnPoints [spawnPointEnd].transform.position.y, 0);
         newDebris.transform.localScale = Vector3.one * randomSize;
 
         /*Vector3 vectorToTarget = (debrisSpawnPoints[spawnPointEnd].position - newDebris.transform.position).normalized;
         float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
         Quaternion q = Quaternion.AngleAxis(angle, Vector3.up);
         transform.rotation = Quaternion.Lerp(transform.rotation, q, Time.deltaTime * speed);*/
 
 
         if (newDebris.transform.position != debrisSpawnPoints [spawnPointEnd].transform.position) {
 
             newDebris.transform.position = Vector3.Lerp (newDebris.transform.position, targetTravel, speed);
 
             Debug.Log ("Hey");
 
         }
 
         yield return new WaitForSeconds (4);
 
         Destroy (newDebris);
         spawned = false;
 
 
     }
 
 }

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

  • Sort: 
avatar image
0

Answer by JC_SummitTech · Dec 07, 2016 at 03:32 AM

First, speed at line 34 is never used, might as well remove it. Second, Lerp just gives an interpolated value. you are currently setting your position once, on start. So nothing should move.

To have something occur overtime, you generally want to put it in Update() or PhysicsUpdate() Update is called every frame, so you could put your position setting code there.

Also, lerp takes 3 params, the start pos, the last pos, and a value between 0 (returning the startPos) and 1 (returning the end pos) so you don't want to give it deltaTime * speed or your return value will be pretty much the same each time, as deltaTime will be almost constant.

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

Follow this Question

Answers Answers and Comments

57 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

Related Questions

Moving instantiated prefab from randomly chosen startPoint to randomly chosen endPoint 1 Answer

rotating/moving colliders with or without rigidbodies? 2 Answers

Help moving an object towards one it is looking at. 1 Answer

Dragging Objects with the mouse 1 Answer

Moving Platforms + Third Person Controller 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