Quantcast
Channel: Questions in topic: "follow"
Viewing all articles
Browse latest Browse all 536

Make an NPC follow player?

$
0
0
![alt text][1]Thank you @lightmaster905 for getting me this far, but i still have questions. I am trying to make my NPC follow a player when it is in the range of the box collider. I added agents, but they dont seem to do anything. The code below is for both the random walking and the seeing player, (until seen comments mean its for the seeing player). thank you so much for your help. using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.AI; using UnityEngine.UIElements; public class Walking : MonoBehaviour { private float latestDirectionChangeTime; private readonly float directionChangeTime = 3f; private float characterVelocity = 2f; //Random.Range(.5f, 3f); private Vector3 movementDirection; private Vector3 movementPerSecond; public GameObject alien; //for the until seen private GameObject player; private Transform target; private float distance; public float sight; private NavMeshAgent agent; void Start(){ latestDirectionChangeTime = 0f; calcuateNewMovementVector(); //for the until seen player = GameObject.FindGameObjectWithTag("Player"); target = player.GetComponent(); agent = GetComponent(); } void calcuateNewMovementVector(){ //create a random direction vector with the magnitude of 1, later multiply it with the velocity of the enemy movementDirection = new Vector3(Random.Range(-1.0f, 1.0f), 0,Random.Range(-1.0f, 1.0f)).normalized; movementPerSecond = movementDirection * characterVelocity; } void Update(){ //if the changeTime was reached, calculate a new movement vector if (Time.time - latestDirectionChangeTime > directionChangeTime) { latestDirectionChangeTime = Time.time; calcuateNewMovementVector(); } //move enemy: transform.position = new Vector3(transform.position.x + (movementPerSecond.x * Time.deltaTime), 22.45f, transform.position.z + (movementPerSecond.z * Time.deltaTime)); //wait a set amount of timeeeeeee //if his movement is to the west, then he turns 90 in the y axis alien.transform.rotation = Quaternion.Slerp (alien.transform.rotation, Quaternion.LookRotation (movementDirection), Time.deltaTime * 4f); distance = Vector3.Distance(target.position, alien.transform.position); while (distance <= sight) { //alien.agent.SetDestination(target.position); agent.SetDestination(target.position); } // Update is called once per frame void UntilSeen() { /* * When the player is 'seen' (goes into the range of the box collider on the NPC) * The NPC will follow the player at 2x speed * * If the player gets out of the NPC's box collider for 10 seconds, * the NPC will slow down and go back to the Walking Script * * i know i need to use waituntil code */ } } } [1]: /storage/temp/153537-screenshot-9.png

Viewing all articles
Browse latest Browse all 536

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>