- Home /
Stop enemy from moving with GetKeyDown
I want to make enemy follow me when they see me and stop when i pressed a certain key. I've tried it and didn't work. The enemy can follow me but it won't stop when i pressed a key. This is my code:
public class SlithMovement : MonoBehaviour
{
private Animator sAnim;
private Transform target;
[SerializeField] private float speed;
[SerializeField] private float maxRange;
[SerializeField] private float minRange;
// Start is called before the first frame update
void Start()
{
sAnim = GetComponent<Animator>();
target = FindObjectOfType<PlayerMovement>().transform;
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(target.position, transform.position) <= maxRange && Vector3.Distance(target.position, transform.position) >= minRange)
{
FollowPlayer();
}
else
{
StopChasing();
}
}
public void FollowPlayer()
{
sAnim.SetFloat("directionX", (target.position.x - transform.position.x));
sAnim.SetFloat("directionY", (target.position.y - transform.position.y));
sAnim.SetBool("isMoving", true);
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
}
public void StopChasing()
{
if (Input.GetKeyDown(KeyCode.G))
{
sAnim.SetBool("isMoving", false);
}
}
}
Answer by lvjuvan · Feb 08, 2020 at 04:51 PM
Currently the StopChasing method is called only if the player is not in range, since it's in the else statement. Since I'm assuming that the enemy should stop chasing the player when the player is out of range I'd rewrite it as
public class SlithMovement : MonoBehaviour
{
private Animator sAnim;
private Transform target;
[SerializeField] private float speed;
[SerializeField] private float maxRange;
[SerializeField] private float minRange;
bool stopped = false;
// Start is called before the first frame update
void Start()
{
sAnim = GetComponent<Animator>();
target = FindObjectOfType<PlayerMovement>().transform;
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(target.position, transform.position) <= maxRange && Vector3.Distance(target.position, transform.position) >= minRange && !stopped)
{
FollowPlayer();
}
else
{
StopChasing();
}
if (Input.GetKeyDown(KeyCode.G))
{
StopChasing();
}
}
public void FollowPlayer()
{
sAnim.SetFloat("directionX", (target.position.x - transform.position.x));
sAnim.SetFloat("directionY", (target.position.y - transform.position.y));
sAnim.SetBool("isMoving", true);
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
}
public void StopChasing()
{
sAnim.SetBool("isMoving", false);
stopped = true;
}
}
wait so you can put two if statement under void update? I'm sorry if i sound dumb, cause i'm new to coding..
Answer by RigbySug · Feb 08, 2020 at 04:51 PM
You have problem in StopChasing() because you are setting animation false. But enenmy still see you.
Let's change like that. I just added one bool.
public class SlithMovement : MonoBehaviour
{
private Animator sAnim;
private Transform target;
[SerializeField] private float speed;
[SerializeField] private float maxRange;
[SerializeField] private float minRange;
private bool StopChk = true;
// Start is called before the first frame update
void Start()
{
sAnim = GetComponent<Animator>();
target = FindObjectOfType<PlayerMovement>().transform;
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(target.position, transform.position) <= maxRange && Vector3.Distance(target.position, transform.position) >= minRange)
{
if(StopChk){
FollowPlayer();
}
}
else
{
StopChasing();
}
}
public void FollowPlayer()
{
sAnim.SetFloat("directionX", (target.position.x - transform.position.x));
sAnim.SetFloat("directionY", (target.position.y - transform.position.y));
sAnim.SetBool("isMoving", true);
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
}
public void StopChasing()
{
if (Input.GetKeyDown(KeyCode.G))
{
sAnim.SetBool("isMoving", false);
StopChk = false;
}
}
}
Answer by Faiz_Abdullah · Feb 09, 2020 at 11:33 AM
I've tried @lvjuvan method and it works perfectly! Thanks for the replies both of you!
Your answer
![](https://koobas.hobune.stream/wayback/20220612222954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycasting AI 0 Answers
Enemy keeps moving in one direction. Help pls! 2 Answers
Enemy AI Not Working? 0 Answers
Looping Between Waypoints 1 Answer