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! using System.Collections; using System.Collections.Generic; using UnityEngine;
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 tormentoarmagedoom · Jan 20, 2019 at 07:19 PM
Good day.
The probelm is at your definition of variables whe you make a variable static.
A static variable means there is only 1 variable of that in all the scene. Not one for each object, only 1 general for all. All objects will acess and read the same variable.
You want that every enemy have its own destination, so that can not be a static variable.
Bye!
Your answer
Follow this Question
Related Questions
FPS controller movement problems 0 Answers
Physics Question 0 Answers
Initialize Prefab with all it's scripts 2 Answers