I recently started working for an IT company and I need to develop a 2D game that allows people to practice what to do in case of a fire. Now one of the things is the player having to get a victim (that is able to walk) out of a room. To do this, I need the so-called victim to follow the player **but only within a certain range**. Now, I watched every existing tutorial out on the great interwebs and looked at the questions and answers on here; nothing. I copy pasted the code but it didn't work. None of them. The only things I reached by it, was the victim either following instantly or not moving at all.
The code I currently have is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Victim : MonoBehaviour {
//public Transform[] patrolPoints;
//Transform currentPatrolPoints;
//int currentPatrolIndex;
public Transform target;
public float chaseRange;
public float speed;
// Use this for initialization
void Start () {
//currentPatrolIndex = 0;
//currentPatrolPoints = patrolPoints [currentPatrolIndex];
}
// Update is called once per frame
void Update () {
//Following player
//See if player (target) is close enough to follow
float distanceToTarget = Vector3.Distance (transform.position, target.position);
if (distanceToTarget < chaseRange) {
//Look at target
Vector3 targetDir = target.position - transform.position;
float angle = Mathf.Atan2 (targetDir.y, targetDir.x) * Mathf.Rad2Deg - 90f;
Quaternion q = Quaternion.AngleAxis (angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards (transform.rotation, q, 180);
//Move towards target
transform.Translate (Vector3.up * Time.deltaTime * speed);
}
}
}
I commented out some things that didnt seem to matter (yet).
All this code does is: nothing.
Yes I have set the player as the target and yes the script is a component of the victim.
Can someone help me by either fixing errors or giving me something entirely new to use, because I really don't want to risk my job on this...
↧