I want to resolve the error I have attached but I don’t know how.
Here is the script where the error takes place (This is the line in question:
Vector2 direction = (Vector2)target.position - rb.position;)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Rigidbody2D))]
public class HomingMissile : MonoBehaviour {
public Transform target;
public float delay ;
public float speed = 5f;
public float rotateSpeed = 200f;
public GameObject ExplosionEffect;
private Rigidbody2D rb;
// Use this for initialization
IEnumerator Start () {
yield return new WaitForSeconds(delay);
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;
}
void OnTriggerEnter2D ()
{
Destroy(gameObject);
Instantiate(ExplosionEffect, transform.position, transform.rotation);
ScoreScript.scoreValue += 1;
}
}