I can't get a "victim" (can be seen as enemy) to follow the player within a certain range
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...
Try this :
public Transform Target;
public float ChaseRange = 5.0f;
public float Speed = 5.0f;
private void Update()
{
var distanceToTarget = Vector3.Distance(Target.position, transform.position);
if (distanceToTarget > ChaseRange)
{
var targetDir = Target.position - transform.position;
var angle = $$anonymous$$athf.Atan2(targetDir.y, targetDir.x) * $$anonymous$$athf.Rad2Deg - 90f;
var q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 180);
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Target.position, Time.deltaTime * Speed);
}
}
Doesn't work. I get the following error:
NullReferenceException: Object reference not set to an instance of an object Victim.Update () (at Assets/Scripts/Victim.cs:13)
I guess Target is not assigned. Did you drag and drop Target in the Editor? Btw which one is line 13?
Your answer
Follow this Question
Related Questions
Boss ai help EoW 0 Answers
Attack/Kill Difficulties 0 Answers
How to make an enemy 2d chasing player? 1 Answer
How to make an enemy 2d chasing player? 2 Answers
How to have multiple enemies (currently having to kill them in order) 1 Answer