- Home /
Position of all spawned prefabs being effected by MoveTowards (beginner question)
Just want to begin by saying that I am an extreme new comer to Unity and game programming in general. So this question is probably going to be extremely obvious, but it is giving me trouble.
So to try and explain my issue as best as possible. My game im working on is a really simple shoot-em up type game where I have prefabs(enemies) spawning from the right side moving left. A gameplay element I have is that the player can occasionally shoot some hacking shots that will give you control of the enemies and they are basically the usually upgrades you would have in a shoot-em up.
My issue comes up when I shoot the enemies with the hacking shot i want to use the MoveTowards to move them to a designated location (emptyobject) by the player. I have it working so that when i shoot a single enemy on screen it will move to place. However, if there are more than one one screen they all start to move to the designated location even if I have only shot one of the enemies.
I feel like there is some way to only manipulate the position of a single prefab at a time and not change all of them together.
Let me know if there is any other info you could use to help solve my issue.
Thanks!
public class enemy2 : MonoBehaviour
{
public int speed;
private Rigidbody2D rb;
public GameObject enemyBullet2;
private int bulletDelay = 6;
private static Transform openSpot;
// Movement speed in units/sec.
public float moveSpeed = 50;
// Time when the movement started.
private float startTime;
// Total distance between the markers.
private float journeyLength;
private static bool beginMove = false;
private
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = Vector2.left * speed;
StartCoroutine(spawnBullet());
}
// Update is called once per frame
IEnumerator spawnBullet()
{
yield return new WaitForSeconds(.5f);
for (int i = 0; i < 4; i++)
{
while (beginMove == false)
{
Instantiate(enemyBullet2, transform.position, Quaternion.identity);
yield return new WaitForSeconds(1);
}
}
}
private void Update()
{
if (beginMove == true)
{
rb.velocity = Vector2.left * 0;
// Move our position a step closer to the target.
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, openSpot.position, 1);
if (gameObject.transform.position == openSpot.position)
{
beginMove = false;
}
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "despawner")
{
Destroy(gameObject);
}
if(col.tag == "Player" && beginMove == false)
{
Destroy(gameObject);
}
if (col.tag == "hacker")
{
if (Spaceship.currentSpot < 6) {
beginMove = true;
openSpot = Spaceship.nextOpenSpot.transform;
}
}
}
}
Answer by xxmariofer · Jan 20, 2019 at 07:44 PM
Hello, even you are spawning them as a prefab, they are all different objects so if each prefab object have that script they should not be affected if only one openSpot position was changed, my guess is that all enemysh are triggering with the hacker bullet, can you put a debug.log inside the ontrigger inside the if (col.tag == hacker) and check how many times is entering when only one of them gets hit?
Thanks for the response!
I added the Debug.Log right inside the if(col.tag == hacker) and it is only getting printed once when I shoot a single enemy.
Ok just checked your code, you are using a static var,
just remove the static leave it:
private Transform openSpot;
you can change openSpot to vector3 and refactor a bit for better performance, if you need help understanding static vars i can give you good examples.
That seems to solve part of my issue. Now only the enemy I shoot moves into spot, which is great.However, now the issue is that when I try to shoot the next enemy and want it to move into the next openspot it does not move there.
down in the if(col.tag == hacker) that is where I have added code which I am iterating through an array every time an enemy is hit by a hackerBullet the enemy will fill in a new designated spot.
my new code is:
if (col.tag == "hacker")
{
Debug.Log("isHit");
if (Spaceship.currentSpot < 6)
{
begin$$anonymous$$ove = true;
openSpot = Spaceship.nextOpenSpot.transform;
Spaceship.incrementOpenSpot();
}
}
Edit: upon further inspection I realized that I once again had my begin$$anonymous$$ove as a static var. I that out and now all the ship are moving fine. However, this brings me to my new issue where it seems like the array, that contains all the empty child objects to my main player that i want to enemies to move, is not being iterated through. So now with every shot all the enemies are moving to the first spot and not the second, third, etc with every additional shot landed.
Your answer
Follow this Question
Related Questions
NPC movement 1 Answer
Moving objects problem 1 Answer
3D Heat Seeking Missile (C#) - Odd problem 1 Answer
simple movement and rotate help! 1 Answer
How do I revert prefab instance properties to their defaults? 0 Answers