My enemy doesn't follow the player. What did I do wrong?,My enemy does not move, he should walk to the player but he doesn't
public void Follow() { if (target.name.ToString()==("Player")) { Debug.Log ("lksfn"); Vector3 dir = target.position - transform.position; // z value is er gewoon omda het 2d is wete wel dir.z = 0.0f; if (dir != Vector3.zero) transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.FromToRotation (Vector3.right, dir), rotationSpeed * Time.deltaTime);
transform.position += (target.position - transform.position).normalized
* moveSpeed * Time.deltaTime;
}
}
Is there a Nav$$anonymous$$eshAgent involved here? I don't follow what you have. I just set the enemy's Nav$$anonymous$$eshAgent to go after the player
using UnityEngine;
using System.Collections;
public class chasePlayer : $$anonymous$$onoBehaviour {
Nav$$anonymous$$eshAgent agent;
public Transform player;
void Start () {
agent = GetComponent<Nav$$anonymous$$eshAgent>();
void Update(){
agent.SetDestination (player.transform.position);
}
}
Nope that isn't involved :/. but I'll definetly google that tomorrow.
tnx :) :) :)
navmesh agent is just a component to put on your enemy. The surface your are on ( the ground) has to be set to static - or at least Navigation static. and just "bake " and unity will place an area for the navmesh to travel. Areas you don't want the enemy to go ( like a wall) make that wall also navigation static Non Walkable. this will carve out a chunk in the navmesh where the enemy can't go. otherwise he will ignore the wall completely and go right through it. Pretty simple stuff but you can get more complicated with it. Like having layers where -depending on the navmesh walkable settings in your enemy navmesh component - some enemies can go and others can't. i have a layer that only my flying drones can travel ( because it goes over water and valleys) Another layer where the drones can travel down hallways - but not go into rooms like the other enemies can. $$anonymous$$ind of fun stuff actually. I spent the day trying to figure out how to change layers at runtime. I have a draw bridge that I want enemies to cross, but only if the bridge is down. So I put that section of the navmesh on a new layer and named it "bridge" and no enemy had nav$$anonymous$$eshWalkable privilidges to it. Until the bridge is lowered then in code would set the walkable$$anonymous$$ask to include that layer so they can run across it
Answer by shadowpuppet · Jan 22, 2018 at 02:48 AM
Here is a solution that works if you don't go by way of NavMeshAgent. Put it on any gameObject to test. You'll see that it has no regard for any obstacles that may get between him and the player - which is fine if it is a ghost and can go through walls. I also added a line at the end so the object stops when it gets too close otherwise it just circles around the player. One last thing is that - at least with my player - the pivot is on the ground , not in his center of mass, so the gameobject is coming at me looking at the ground. But he is moving and chasing and stops. NavMesh is a much better way
using UnityEngine;
using System.Collections;
public class chasePlayer : MonoBehaviour {
float moveSpeed = 3.0f;
public GameObject player;//note the small case "p"
private Transform Player;//note the upper case "P". You don't need this to make the object follow the player but I put it in in a hurry to do the last bit of code to get him to stop.
public int stopDist = 2;
void Start(){
Player = player.transform;
}
void Update(){
//code for looking to player
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(player.transform.position - transform.position), moveSpeed * Time.deltaTime);
//code for following the player
transform.position += transform.forward * moveSpeed * Time.deltaTime;
// the next part below is what I through in to get the object to stop if too close then resume if player moves away again
if (Vector3.Distance (transform.position, Player.position) <= stopDist)
moveSpeed = 0f;
if (Vector3.Distance (transform.position, Player.position) > stopDist)
moveSpeed = 3f;
}
}
Your answer

Follow this Question
Related Questions
Rigidbody2D not working 0 Answers
Having problems with Overlap Circle 0 Answers
2D platforms. Shoot towards mouse position 0 Answers
Why is my sprite only detecting one sprite as the ground? 1 Answer
2D Side scorller, moves but wont jump 0 Answers