- Home /
Question by
rOBY GAMES · May 15, 2015 at 07:24 PM ·
playerenemytarget
Enemy does not look the player 2D platformer.
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;
}
}
}
Comment
Answer by InsertFunnyUsernameHere · May 15, 2015 at 08:19 PM
Try myTransform.LookAt(target.position)
Which line should I add? I tried everywhere, but it always fails
Your answer
![](https://koobas.hobune.stream/wayback/20220613195705im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
enemy targeting player in different scene 1 Answer
Enemy spawn then select target? 1 Answer
Enemy targeting player in different scene 1 Answer
Distance destroy object 3 Answers