Hi all, I am making a horror game with a random walking NPC.
When the NPC sees the character, (when the player goes in range of the NPC's attached box collider), I want the NPC to start following the player for 10 seconds.
The comments![alt text][1] for the code are at the bottom of my script, the rest is the random walking I coded.
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;
void Start(){
latestDirectionChangeTime = 0f;
calcuateNewMovementVector();
}
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);
// 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
*/
}}}}
I attached an image of that the box collider will look like.
I have walls in the game, and want to make sure that the collider cannot "see" through walls also. Thank you so much in advance!
[1]: /storage/temp/153344-screenshot-9.png
↧