How to move an enemy towards the player in Unity3D

When you make a game, you often need to make enemies that move towards the player.

In this video I’ll share some method to move an enemy object towards the player in Unity3D.

Watch this video to see how to do it by creating a simple script:

And here is the script used on this video:

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

public class enemymover : MonoBehaviour {

    public GameObject player;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        //transform.LookAt(player.transform);
        Quaternion targetRotation = Quaternion.LookRotation(player.transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 1 * Time.deltaTime);
        transform.position += transform.forward * 1f * Time.deltaTime;
	}
}

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *