- Home /
Enemy follow Camera
I've got the following script attached to the enemy to make it move towards the camera:
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour
{
Transform player;
NavMeshAgent nav;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("MainCamera").transform;
nav = GetComponent <NavMeshAgent> ();
}
void Update ()
{
nav.SetDestination (player.position);
}
}
The problem I have is that it only follows the camera if it get close to the enemy on a flat plain; it won't follow the camera into 3D space. The scene has a navmesh (and the enemy a navmesh agent) which I guess is causing the problem. But I also tried duplicating the model, removing the navmesh agent and using this script:
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour
{
Transform player;
public float speed = 6f;
GameObject enemy;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("MainCamera").transform;
enemy = GameObject.Find("Enemy");
}
void Update ()
{
enemy.transform.LookAt(player.position);
enemy.transform.position += transform.forward * speed * Time.deltaTime;
}
}
But this did nothing. How can a make the enemy move towards the camera, wherever it goes?
Your answer
Follow this Question
Related Questions
Nav Mesh problem~ I need help! 0 Answers
NavMeshAgent not working, working for some agents 0 Answers
navmesh.sampleposition works strangely, what is going on here? 0 Answers
NavMesh giving jerky like motion 0 Answers
how to make navmeshagent walk smarter 0 Answers