I have now added the main mechanic of the enemy to follow the player. I got the code from this site but I have added some modifications and fixed errors in the code. This is the code below:
using UnityEngine;
using System.Collections;
public class EnemyFollow : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxdistance;
private Transform myTransform;
void Start(){
myTransform = transform;
}
void Update () {
if (Vector3.Distance(target.position, myTransform.position) < maxdistance)
{
// Get a direction vector from us to the target
Vector3 dir = target.position - myTransform.position;
// Normalize it so that it's a unit direction vector
dir.Normalize();
// Move ourselves in that direction
myTransform.position += dir * moveSpeed * Time.deltaTime;
}
}
}
The distance from the player to begin movement of the object and speed of the object can be edited on the right panel of the script as shown below:
No comments:
Post a Comment