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 mobee555 · Feb 28, 2017 at 11:32 AM · instantiate prefabspawning problemsinstantiating

Spawning Cars according to the position of Player Car

In my Car Game i have a player car and enemy cars. Initially I Instantiated the cars at some random positions but the issue is, i want, as the terrain moves forward the position of the cars at which they will instantiate moves accordingly with the player car within some radius.

Comment
Add comment · Show 3
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 BTCallahan · Feb 28, 2017 at 01:47 PM 0
Share

@mobee555 I'm not sure what you mean. Is the car stationary and you want the spawn positions to move toward the car if the player accelerates?

avatar image mobee555 BTCallahan · Mar 15, 2017 at 06:29 AM 0
Share

$$anonymous$$y player car is moving and enemy cars are spawned for the first time. no issues. When i destroy the enemy cars, then surely there will be a need to re-spawn the enemy cars. I want to re-spawn the enemy cars according to the moving player position.

avatar image hgaur725 · Mar 15, 2017 at 06:43 AM 0
Share

@mobee555 You have to track the position of the player car and when you instantiate the enemies then you must have to set the position of the enemies according to the player car. Or you can put the spawner empty game object in the car and then set distance accordingly to instantiate the enemy.

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mughees_Mehdi · Mar 15, 2017 at 09:23 AM

You can use OnBecameVisible Function of mesh renderer of the Enemy Cars whenever they are not visible in the camera after being hit and can reset them again.

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     void OnBecameInvisible() {
        
     }
 }












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

Answer by hexagonius · Mar 15, 2017 at 07:59 AM

on spawn, use the player position and add a vector to it with random x and z, and y = 0. random -1 to 1 (float) should suffice. normalize that vector and multiply it by the distance you want cars to spawn at from the player.

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

Answer by hgaur725 · Mar 15, 2017 at 01:50 PM

@mobee555 You have to track the position of the player car and when you instantiate the enemies then you must have to set the position of the enemies according to the player car. Or you can put the spawner empty game object in the car and then set distance accordingly to instantiate the enemy.

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

Answer by ifdef_ben · Mar 15, 2017 at 03:29 PM

It's not a radius because I'd have to dig how to select a random point in a circle, but doing this in a square-shaped area around the player is fairly straightforward..

 float radius = 25f; //The "radius" of your square area here.
 float xOffset = Random.Range(-radius, radius);
 float zOffset = Random.Range(-radius, radius);
 Vector3 playerPosition = player.transform.position;
 Vector3 spawnPosition = new Vector3(playerPosition.x + xOffset, playerPosition.y, playerPosition.z +zOffset);
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
avatar image
0

Answer by BTCallahan · Mar 19, 2017 at 04:59 PM

@mobee555 I came up with a solution that might be what you're looking for. It's not perfect, you're still going to have to tell it the rotation of the car that gets spawned, and the Y distance (I'm assuming that you're not using a level track).

 public void RandomSpawn(float minRange, float maxRange, GameObject player, Quaternion carRotation, float carYdistance, params GameObject[] carsToSpawn){
 
 float spawnRads = Mathf.Deg2Rad * Random.Range(0f, 359f);
 float spawnDistance = Random.Range(minRange, maxRange);
 float x = player.transform.position.x + Mathf.Cos(spawnRads) * spawnDistance;
 float z = player.transform.position.z + Mathf.Sin(spawnRads) * spawnDistance;
 
 GameObject.Instantiate<GameObject>(carsToSpawn[Random.Range(0, carsToSpawn.Length)], new Vector3(x, carYdistance, z), carRotation);
 }
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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiated objects becomes child of spawn point 0 Answers

Spawn multiple enemies in relation to gameobject [SOLVED] 1 Answer

spawn from pool only if pool has inactive gameobjects 3 Answers

How to check for the nearest gameObject in OverLapCircleAll? 1 Answer

Collision not detecting between two instantiated objects 1 Answer


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