This question was
closed Nov 29, 2016 at 07:44 AM by
destro22 for the following reason:
The question is answered, right answer was accepted
Question by
destro22 · Nov 28, 2016 at 10:45 PM ·
c#movement scripttriggersfps controllerenemyai
How do I stop enemy AI from bumping and entering into my player?
Hey, I am trying to stop the AI enemy from bumping into my player and staying at a distance but unable to do so. can you please help me out?? I have colliders attached on both but trigger is off.
I am attaching the script which i am using for the enemy ai to chase my player
capture.png
(26.6 kB)
Comment
Best Answer
Answer by Filhanteraren · Nov 29, 2016 at 01:22 AM
using UnityEngine;
public class KeepDistance : MonoBehaviour
{
public Transform player;
public float desiredDistance;
private void Update()
{
var offset = this.transform.position - player.position;
if (offset.sqrMagnitude < desiredDistance * desiredDistance)
this.transform.position = this.transform.position + offset.normalized * (desiredDistance - offset.magnitude);
}
}
This will keep an object at the desired distance to another object.
Follow this Question
Related Questions
Script makes Uniy Freeze 0 Answers
Switching lanes 1 Answer
How to check if player remains on a path? (2D) 1 Answer
How do I add running and animations to my FPS controller script? 0 Answers