Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Spawn objects infront of moving player

Discussion in 'Scripting' started by kipa1996, Jun 21, 2018.

  1. kipa1996

    kipa1996

    Joined:
    Jun 21, 2018
    Posts:
    1
    Hello there!

    Im new to making games and have just recently downloaded unity and followed Brackeys tutorial on youtube on how to make your first video game. This video game is the one where you are a cube moving down a path dodging other cubes that are in the way. I've been able to keep up on the tutorial so far but now I want to go a little off tutorial. I've been able to make 5 spawnpoints where 4 of them spawn randomly and appear in certain locations infront of the player at the beginning but I really dont know how to make it so they continuously spawn randomly infront of the player as it moves forward. Here's my code so far inte the spawning script. Just tell me if u need anything else since this is my first post here.

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4.  
    5. public class BlackSpawner : MonoBehaviour {
    6.  
    7.     public Transform[] SpawnPoints;
    8.  
    9.     public GameObject blockPrefab;
    10.    
    11.     void Start ()
    12.     {
    13.         int RandomIndex = Random.Range(0, SpawnPoints.Length);
    14.  
    15.         for (int i = 0; i < SpawnPoints.Length; i++)
    16.  
    17.         {
    18.             if (RandomIndex != i)
    19.             {
    20.                 Instantiate(blockPrefab, SpawnPoints[i].position, Quaternion.identity);
    21.  
    22.             }
    23.  
    24.  
    25.         }
    26.     }
    27.  
    28.  
    29.    
    30. }
    31.  
    32.  
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You need to learn how to decompose complex task to much smaller ones.
    If I do understand correctly you would need to:
    1. Determine where player actually heading. If this one is always Z+ then direction would be Vector3.Forward (World Space Forward)
    2. Use .Update() / Coroutine to create a timer that detects when to spawn prefabs.
    3. At the moment of spawn - Instantiate your object in front of the player. Either pick direction from player script or use Vector3.Forward.
    4. Shift position for the spawn along the direction

    E.g.
    Code (CSharp):
    1. Player.position + direction * distance.
    2.   Where: Direction would be either Player Forward of World Forward (Vector3.Forward)
    3.   Distance is distance from the player where to spawn.
    5. Offset your position on X \ Y \ Z axis via Random.Range, or if you prefer all together - Random.insideUnitSphere / .onUnitSphere.

    6. Now you'll have the position to instantiate the object.

    Do your research, learning is fun. You can pretty much google through how to do it.
     
    SharpBranch likes this.
  3. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    The easiest way where you don't even need to code: put the spawn points inside of the player game object (so that they are children of the player main object. If the player moves / rotates the spawn points are moved automatically along with it because they are attached.

    If you don't want to place them inside the player object you can change the position of them via code at the same time and amount as the player position.
    Alternatively you could also get rid of the transform and calculate the positions yourself to have always different spots in relation to the player position (for that approach you would add a random Vector to
    myPlayerObject.transform.forward
    and add also
    myPlayerObject.transform.position
    .