- Home /
Question by
Valentinon13 · Oct 10, 2016 at 07:23 PM ·
playerenemy
How to solve !
im working on a enemy script to chase te player if i start my game the enemy is already following my player but the player is not in range how to solve it !
using UnityEngine; using System.Collections;
public class MOB3 : MonoBehaviour
{
public Animator anim;
public float speed;
public float range;
public CharacterController controller;
public Transform player;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if (inRange())
{
transform.LookAt(player.position);
anim.SetBool("walk", true);
}
else if (!inRange())
{
Chase();
anim.SetBool("walk", false);
}
}
bool inRange()
{
if (Vector3.Distance(transform.position, player.position) < range)
{
return true;
}
else
{
return false;
}
}
void Chase()
{
transform.LookAt(player.position);
controller.SimpleMove(transform.forward * speed);
anim.SetFloat("Speed", 1);
}
}
Comment
Best Answer
Answer by Arcana96 · Oct 10, 2016 at 08:12 PM
@Valentinon13 You're calling the chase method when the player is not in range. Your update should look like:
void Update()
{
if (inRange())
{
transform.LookAt(player.position);
Chase();
anim.SetBool("walk", true);
}
else if (!inRange())
{
anim.SetBool("walk", false);
}
}
Thanks dude you are a life saver :P !!! one more qeustion im using the Animator but the
Severity Code Description Project File Line Suppression State Warning Parameter 'Speed' does not exist. Solution 'Blande 0.1v' (1 project) Assets/__TEST/Scripts/$$anonymous$$OB3.cs 51
Have you spelt the parameter name correctly?