,Attack and chase range issue
Hi, I'm new to coding and I can't figure out why this code won't work for me. The Range is detecting the distance between the enemy and the player correctly but it won't trigger. I'd appreciate if anyone had any insights.
Chaserange is set to 10 and attackrange is set to 2. Thanks.
using UnityEngine; using System.Collections;
public class Mob : MonoBehaviour { public int health; public float speed; public float chaserange; public float attackrange; private float Range;
public CharacterController controller;
public Transform player;
public Animator anim;
// Use this for initialization
void Start ()
// Update is called once per frame
void Update ()
{
Range = (Vector3.Distance (transform.position, player.position));
Debug.Log (Range);
}
void chaseplayer()
{
if (Range <= chaserange && Range > attackrange)
{
transform.LookAt (player.position);
controller.SimpleMove (transform.forward * speed);
anim.SetBool ("isAttacking", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isRunning", true);
}
}
void attackplayer()
{
if (Range <= attackrange)
{
transform.LookAt (player.position);
anim.SetBool ("isAttacking", true);
anim.SetBool ("isIdle", false);
anim.SetBool ("isRunning", false);
}
}
void OnMouseOver()
{
player.GetComponent<Fighter> ().opponent = gameObject;
}
}
Answer by nathanlink169 · May 28, 2017 at 11:55 AM
It looks like your issue is that you're not calling the chaseplayer function. Inside of your Update function, simply add the line chaseplayer();
Answer by Unity5License · May 28, 2017 at 12:01 PM
That worked perfectly! Thank you I've been trying to figure it out for hours :D
Your answer
Follow this Question
Related Questions
How can I add both animator and script rotation on Game object? 0 Answers
In my script the Animation selection pop up shows no assets even though i have several? 0 Answers
Door animation bugs and stays open unity 3D 0 Answers
help with particle projector 0 Answers
,Why do I get the same value from object property? 0 Answers