Search Unity

Spawned Cubes go crazy and sometimes move away from the player.

Discussion in 'Scripting' started by tomowale, Mar 20, 2015.

  1. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    New thread

    Basically I have a player and two empty game objects . When the cubes spawn they move away from the player in a crooked line any way to fix this.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
    Can you show your script? Or screenshot or more info how they are moved?
     
  3. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    OK code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CreateCube : MonoBehaviour {
    5.     public GameObject Car;
    6.     public Transform Player;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         StartCoroutine (EnemySpawn ());
    11.     }
    12.    
    13.     IEnumerator EnemySpawn()
    14.     {
    15.         while (true) {
    16.             Instantiate (Car, transform.position, Quaternion.identity);
    17.             yield return new WaitForSeconds (1);
    18.         }
    19.  
    20.     }
    21. }
    22.  
     
  4. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Photo
     

    Attached Files:

  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
    How the cubes are getting moved?
    Or you want to spawn in straight line?
     
  6. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Yeah , I want them to move in a straight line so the player can dodge them.
     
  7. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    *Straight line towards the player.
     
  8. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Movement code please
     
  9. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Here is the deal , I created a script where the object moves forward automatically in a straight line.Tried it out on a cube it worked. Attached it to the empty game objects that the objects were spawning off of.It worked now the "cars" move straight , so I figured that out, but it moves away from the player. Any ideas how to fix that?
     
  10. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    No idea, post the code which makes it happen.
     
  11. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Attached this to the empty gameobjects which spawn the cubes , and they move in a straight line now, but away from the player.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveFoward : MonoBehaviour {
    5.     public float moveSpeed = 10f;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.         transform.position += transform.forward * Time.deltaTime * moveSpeed;
    16.     }
    17. }
    18.  
     
  12. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Are you sure the forward direction is towards the player? The cubes may not be oriented correctly when spawning.
     
  13. Xatu1992

    Xatu1992

    Joined:
    Mar 22, 2015
    Posts:
    13
    This might help you understand a little better.


    Code (JavaScript):
    1. var prefab : GameObject;
    2. var gridX = 5;
    3. var gridY = 5;
    4. var gridZ = 5;
    5. var spacing = 1;
    6.  
    7. function Start ()
    8. {
    9.                 for (var y = 0; y < gridY; y++){
    10.                 for (var x = 0; x < gridX; x++){
    11.                 for (var z = 0; z < gridZ; z++){
    12.        
    13.                 var pos = Vector3(x,y,z) * spacing;
    14.                 Instantiate(prefab, pos, Quaternion.identity);
    15.        
    16.        
    17.        
    18.             }
    19.         }
    20.     }
    21. }
    22.  
     
    tomowale likes this.
  14. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    That's exactly my point the cubes are moving AWAY from the player instead of looking right at him and heading towards him.
     
  15. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    It helped me a bit , but I don't understand much JS as I use C#.
     
  16. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Wait, you never specified what direction the cube should look. You just said quaternion.identity is the rotation.

    Instead of doing that, in the start method, make the cube turn to face the player. Because apparently right now it's just spawning with a rotation of 0,0,0,0, and that will only be the direction of the player if they specifically step in that line.

    You could also turn the spawning object to face the player, and when you instantiate the objects, set them to have the same rotation as the parent.
     
  17. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Ok , I get what you're saying, would transform.lookat(Player) work?
     
  18. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    For sure. Well, I think it takes a vector 3 but still
     
  19. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Vector 3 what exactly 0,2,0?
     
  20. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Yup you are probably right, I hope you can correct my script. I used transform.Lookat(Player) Cubes started spawning EVERYWHERE , on the player ,In the sky LOL.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CreateCube : MonoBehaviour {
    5.     public GameObject Car;
    6.     public Transform Player;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         StartCoroutine (EnemySpawn ());
    11.     }
    12.    
    13.     IEnumerator EnemySpawn()
    14.     {
    15.         while (true) {
    16.             Instantiate (Car, transform.position, Quaternion.identity);
    17.             yield return new WaitForSeconds (1);
    18.             transform.LookAt(Player);
    19.         }
    20.  
    21.     }
    22. }
    23.  
     
  21. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    You're not setting the car's transform to look at the player, you're setting the spawner object to look at the player

    Change line 16 to "GameObject newcar = (GameObject)Instantiate(Car, transform.position,Quaternion.identity);"
    This will save the reference so you can use it later, so you can use this to tell the newly spawned car to look at the player.

    newcar.transform.LookAt(player);
     
  22. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Ok , the cubes spawned ,but only the first two came forward to the player and then crossed pathso_O.And sometimes all the cubes come forward and basically jumble together.This is gonna cause a problem considering I am doing a car game.The spawn "cars" follow the player I see, which is not really what I want, I just want the objects to spawn far away from another so the player can dodge them, Should I scrap this and just let the objects spawn randomly on the map? I hope I can get your feedback.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CreateCube : MonoBehaviour {
    5.     public GameObject Car;
    6.     public Transform Player;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         StartCoroutine (EnemySpawn ());
    11.     }
    12.  
    13.     IEnumerator EnemySpawn()
    14.     {
    15.         while (true) {
    16.             GameObject newcar = (GameObject)Instantiate(Car,transform.position,Quaternion.identity);
    17.             yield return new WaitForSeconds (1);
    18.             newcar.transform.LookAt(Player);
    19.         }
    20.  
    21.     }
    22. }
    23.  
     
    Last edited: Mar 28, 2015
  23. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Photo of cubes going crazy.
     

    Attached Files:

  24. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Anyone?
     
  25. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Asked around but it seems my issue is unfixable.
     
  26. louisgv

    louisgv

    Joined:
    Aug 7, 2013
    Posts:
    18
    Code (CSharp):
    1.  GameObject newcar = (GameObject)Instantiate(Car,transform.position,Quaternion.identity);
    For the position of the spawned cars to be separated, you should write a method to generate position (i.e return a Vector3 around the player, multiply a distance by Random.AroundUnitCircle etc..), and replace transform.position with the result of that method.

    For the problem of moving toward player, just plug a negative sign and play around with the direction you observe. You have to do some experiment.

    P/s: Avoid double|triple post as those are signals of someone unfriendly.
     
  27. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    @tomowale,

    Consider making a copy of your project and reducing it to the bare minimum that demonstrates your issues.
    Upload the package here ( or elsewhere and provide a link)
    This will reduce confusion and guessing (you will need to provide an unambiguous description of your expectations and problems ) ...

    Regards,
     
  28. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    @louisgv Is there any documentation on the Random.AroundUnitCircle?(Because If I don't have any , I would just have to ask what it is which would be annoying for you) Couldn't seem to find any .Oh and also for Vector 3 do you mean just replace transform.position with Vector3 (0,2,0) or something like that?

    P.s Won't do that again then, didn't know it meant so much, really was just desperate for an answer.:(
     
    Last edited: Apr 1, 2015
  29. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    @kdub Good idea.What would you say is the bare minimum?
     
  30. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Just enough to demonstrate the issue ... without all the bandwidth gobbling extras :)

    ... but test it in case you accidentally have fixed the problem. ;)
     
    tomowale likes this.
  31. louisgv

    louisgv

    Joined:
    Aug 7, 2013
    Posts:
    18
    Sorry for the wrong name :p. What you wanted to experiment with is this guy:
    http://docs.unity3d.com/ScriptReference/Random-insideUnitCircle.html
    Yeah. But that Vector3 (0,2,0) you declared won't be dynamic enough, i.e you will just have your car spawn at that one point only.

    Pass in Random.insideUnitCircle multiplied by a delta distance should do the trick
    P/s: To avoid triple|double post, just click edit your post. Unless your thread was abandoned for more than a week then it would be reasonable to pull it up by double post ;)
     
    tomowale likes this.
  32. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    If this is what you meant.Tried it out , cubes just decided not to spawn at all (cuz of transform.position missing)

    Code (CSharp):
    1. GameObject newcar = (GameObject)Instantiate(Car,Random.insideUnitCircle*5,Quaternion.identity);
     
    Last edited: Apr 8, 2015
  33. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    One question though what does Random.InsideUnitCircle exactly do? Does it spawn objects in a specific area?
     
  34. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  35. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    "with radius 1" not to sure what that means. But will post some code that I tried out later ( Lol Unity refused to let me post some code so will try later)
     
  36. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I made some new code but no cubes appear.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CreateCube : MonoBehaviour {
    5.     public GameObject Car;
    6.     public Transform Player;
    7.     public Vector3 newPosition = Random.insideUnitCircle * 6;
    8.     // Use this for initialization
    9.     void Start () {
    10.         StartCoroutine (EnemySpawn ());
    11.     }
    12.    
    13.     IEnumerator EnemySpawn()
    14.     {
    15.         while (true) {
    16.             GameObject newcar = (GameObject)Instantiate(Car,transform.position,Quaternion.identity);
    17.             yield return new WaitForSeconds (1);
    18.             newcar.transform.LookAt(Player);
    19.         }
    20.         transform.position.x = newPosition.x;
    21.         transform.position.y = newPosition.y;
    22.        
    23.  
    24. }
    25. }
     
  37. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Unity gives me the errors of the new code is unreachable , and
    "Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable". Here is the new code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CreateCube : MonoBehaviour {
    5.     public GameObject Car;
    6.     public Transform Player;
    7.     public Vector3 newPosition = Random.insideUnitCircle * 6;
    8.     // Use this for initialization
    9.     void Start () {
    10.         StartCoroutine (EnemySpawn ());
    11.     }
    12.    
    13.     IEnumerator EnemySpawn()
    14.     {
    15.         while (true) {
    16.             GameObject newcar = (GameObject)Instantiate(Car,transform.position,Quaternion.identity);
    17.             yield return new WaitForSeconds (1);
    18.             newcar.transform.LookAt(Player);
    19.         }
    20.         transform.position.x = newPosition.x;
    21.         transform.position.y = newPosition.y;
    22.        
    23.  
    24. }
    25. }
     
  38. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
     
    tomowale likes this.
  39. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Well the cubes don't actually spawn after I used your code , 2 cubes just appear infront of each other (heading away from the player) and the old 2 cubes disappear. Some even fall off the edge, it kinda looks cool though lol. Did you test the code?
    Should I upload footage of the issue? If so how.
    OR
    Should I move onto the the actual building of my game and use a new method to spawn cars,now or continue prototyping?
     

    Attached Files:

    • 6.png
      6.png
      File size:
      112.7 KB
      Views:
      673
  40. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Does anyone know how some creators manage to make cars come and go in different directions and spawn flawlessly into the scene view and then start moving?