Hello! Thank you for the help in advanced! Im new to unity so im struggling a bit.
Im trying to get a bullet to be created randomly and as soon as it spawns to move towards the players position right when it spawns, but it seems to be constantly moving towards the player without stopping until it hits the player.
I have been trying for hours to work my way around this and finding the coordinates of the player when it spawns, but even then it is failling.
public class MoveTowardsPlayer : MonoBehaviour
{
[SerializeField] private GameObject bullet;
public float speed = 5f * Time.deltaTime;
public Transform player;
private bool follow;
// Start is called before the first frame update
private void Start()
{
}
// Update is called once per frame
public void Update()
{
transform.position = Vector3.MoveTowards(transform.position, player.position, speed);
}
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "Ground")
{
//Debug.Log("Bullet Touching Ground");
Object.Destroy(bullet);
}
if (collision.collider.tag == "Player")
{
Debug.Log("Touched Player");
Object.Destroy(bullet);
}
}
}