Quantcast
Viewing latest article 10
Browse Latest Browse All 1488

How can i stop the enemy jitter when it follow the player?

Hi folks! I have been working on the enemy script from 2 days, and unable to solve this issue. I have write a code where the enemy(object) can follow the moving player. I had also added distance limits so the enemy can't collide with player, this is what making the problem. whenever enemy hit the minimum distance from player, it start jittering. here is the code I write for the enemy to follow the player: using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; public class follow_player : MonoBehaviour { public Transform player; public float speed; public Transform start_point; public bool attack_player = false; public float Distance_between; private Rigidbody enemy_rb; // Start is called before the first frame update void Start() { enemy_rb = GetComponent(); } // Update is called once per frame void FixedUpdate() { float dist = Vector3.Distance(transform.position, player.position); if(dist > Distance_between) { if (attack_player == true) transform.position = Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime); } else if(dist < Distance_between) { transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z); } if (attack_player == false) transform.position = new Vector3(start_point.position.x, start_point.position.y, start_point.position.z); } }

Viewing latest article 10
Browse Latest Browse All 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>