Hello everyone.
I'm trying to write a C # combining various scripts.
What I want to do is allow the enemy to look over the player to follow.
This does not work now. The player itself is the enemy right look left, so he turns to the player, but walks like Michael Jackson (MoonWalker) to the player. Now I would like the enemy is aimed towards the player while chasing him.
Any suggestions?
script:
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 Awake()
{
myTransform = transform;
}
void Start ()
{
maxdistance = 2;
}
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;
}
}
}
↧







