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 Seby442 · Jun 24, 2018 at 09:54 PM · instantiatevector3instantiate prefab

Spawning prefabs in front of the player

So what i am trying to do is make a script that tracks the player's movement and spawns in front of him random prefab obstacles around 20 units ahead of him (just out of his line of sight) and I am not sure how to do that since the current script only spawns the object at the initial location of the player and that's all. Second thing would be how to spawn the ground for the player a few units before he reaches the end of the first ground.

 public GameObject prefab1, prefab2, prefab3, prefab4, prefab5;


 public float SpawnRate = 0f;

 public GameObject myPlayer;

 private Vector3 GetXAxis()
 {
     return new Vector3(myPlayer.transform.position.x, 0, 0);

 }
 private void Update()
 {
     Vector3 xVector = GetXAxis(); // getting that vector from above
 }
 public float nextSpawn = -3f;

  
 int WhatToSpawn;


 private void FixedUpdate()
 {
     if (Time.time > nextSpawn)
     {
         WhatToSpawn = Random.Range(1, 3);
         Debug.Log(WhatToSpawn);





         switch (WhatToSpawn)
         {
             case 1:
                 Instantiate(prefab1, transform.position = GetXAxis() * Time.fixedDeltaTime * 20 , Quaternion.identity);
              break;
             case 2:
                 Instantiate(prefab2,GetXAxis() * 20 * Time.fixedDeltaTime , Quaternion.identity);
                 break;
           /*  case 3:
                 Instantiate(prefab3, transform.position, Quaternion.identity);
                 break;
             case 4:
                 Instantiate(prefab4, transform.position, Quaternion.identity);
                 break;
             case 5:
                 Instantiate(prefab5, transform.position, Quaternion.identity);
                 break;
           */
         }
         nextSpawn = Time.time + SpawnRate;
     }
 }


}

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

Answer by Trevdevs · Jun 24, 2018 at 11:44 PM

Vector3 spawnPos = playerPos + offset

in your case offset = new Vector3(0, 0, 20);

Also here is a very basic spawn system i whipping up for ya in about 5 minutes not testing so let me know if it doesnt work.

 using UnityEngine;
 
 public class SpawnThing : MonoBehaviour 
 {
     public Vector2 xRange;
     public Vector2 yRange;
     public GameObject[] objectsToSpawn;
     public float spawnRate;
     public float distanceToSpawnFromPlayer;
     public Transform player;
 
     private float currentTime;
 
     void Update()
     {
         if(Time.time > currentTime)
         {
             Spawn(objectsToSpawn[Random.Range(0, objectsToSpawn.Length)]);
             currentTime = Time.time + spawnRate;
         }
     }
 
     void Spawn(GameObject spawnObject)
     {
         Vector3 spawnPos = player.position;
         spawnPos.x += Random.Range(xRange.x, xRange.y);
         spawnPos.y += Random.Range(yRange.x, yRange.y);
         spawnPos.z += distanceToSpawnFromPlayer;
 
         Instantiate(spawnObject, spawnPos, Quaternion.identity);
     }
 }


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

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

125 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate an object every x meters between two others. 1 Answer

Get rotation from hit object 1 Answer

Projectiles trigger affecting other projectiles of same prefab - C# 0 Answers

Why is it important to create an empty gameobject for my prefabs? 0 Answers

Instatiating at different x Positions error 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