The error is because the homing bullets don’t have a target when the player dies and it comes with an error, please help me fix
How do I fix this?
Bullet script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent( typeof(Rigidbody2D) )]
public class HomingMissile : MonoBehaviour
{
public Transform target;
public float speed = 5f;
public float rotateSpeed = 200f;
private Rigidbody2D rb;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Vector2 direction = (Vector2)target.position - rb.position;
direction.Normalize();
float rotateAmount = Vector3.Cross(direction, transform.up).z;
rb.angularVelocity = -rotateAmount * rotateSpeed;
rb.velocity = transform.up * speed;
}
}