- Home /
need help with enemy respawn
hi i am new in game dev and coding so i am making a 2d game and its a simple one i have 2 game objects A and B and they are moving forward without stoping (A chasing B) so when A tuch B it will destroyed the problem i need the object B to respawn forward so A can chase it again becuse i use infinity background and i cant use respawn point so please can you help me and make a script for that A= player B = enemy
thank you
Sounds like you just want the player to respawn in front of the enemy? You can instantiate a new gameobject or set the previous one back to active with a transform equal to that of the enemies plus some small spacing vector. Is this what you're trying to do?
Answer by morian35 · Jun 06, 2020 at 01:03 AM
no i want the enemy to respawn in front of the player
so can you write a code that make the enemy respawn in the front of the player (even if the player is moving )
Answer by nikigmi · Jun 06, 2020 at 01:16 AM
If I understand correctly you don't have a problem with the spawning itself but where to spawn the new enemy. If you want to spawn and enemy in front of the player you can just calculate the new spawn point based on the player position like so:
vector3 spawnPoint = playerObject.transform.position + offset
You can calculate the offset like this if your characters are moving along the x axis: If not, pass the offsetDistance to the right axis of offset
float offsetDistance = 5;
Vector3 offset = new Vector3(offsetDistance,0,0);
Also for objects that are frequently destroyed and created its advised to use object pools for performance reasons. This may help: https://www.raywenderlich.com/847-object-pooling-in-unity
i can just destroy the object i dont know how to make a script for respawn it.so can you help me to write respawn script
thank you
Firstly the forums are not a code writing service. Secondly, I don't know your project, how you move your characters, what your scene setup is like etc.
If you don't know much about unity or program$$anonymous$$g I suggest you find a tutorials to learn the basics. You can find some made by unity here: https://learn.unity.com/ The "New to Unity" course is 3-4 hours and should get you going. https://learn.unity.com/course/getting-started-with-unity
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Player is jittery when it collides with wall (C# 3D) 0 Answers
OnCollisionEnter Sine Wave to Character 0 Answers
Need Help Making A 2d Grappling hook 3 Answers