You are on page 1of 1

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class HomingScript : MonoBehaviour
{

public Transform target;

public float speed = 5f;


public float rotateSpeed = 200f;
public GameObject ExsplosionEffetc;
private Rigidbody2D rb;

// Use this for initialization


void Start()
{

rb = GetComponent<Rigidbody2D>();
}

void FixedUpdate()
{

target = GameObject.FindGameObjectWithTag("Player").transform;
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()
{
Instantiate(ExsplosionEffetc, transform.position, transform.rotation);
Destroy(gameObject);
}
}

You might also like