I have a rocket that follows the player around. I made a spawner script that spawns the rockets with equal intervals. I am running into a problem and that it that, the rockets do spawn, but they can’t access player or look for the player in the scene. Any idea on how to fix this? Also, my game is one of those endless rocket dodging games.
here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class MissileBehaviour : MonoBehaviour
{
public GameObject explosionVFX;
public Transform target;
public float speed = 5f;
public float rotateSpeed = 200f;
private Rigidbody2D rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Vector2 direction = (Vector2)target.position - rb.position; //getting direction
direction.Normalize();
float rotateAmount = Vector3.Cross(direction, transform.up).z;
rb.angularVelocity = -rotateAmount * rotateSpeed; //angular velocity
rb.velocity = transform.up * speed; //adding velocity based on global axis
}
void OnTriggerEnter2D()
{
GameObject explosionObject = Instantiate(explosionVFX, transform.position, Quaternion.identity);
Destroy(gameObject);
Destroy(explosionObject, .5f);
}
}