x


Follow Player when Close C# Help

Hi Guys! :) I have a script here and i need help how do i do so The AI Follows player just when the player is Close? I have a Script Temp Already here :D :

SCRIPT

using UnityEngine; using System.Collections;

public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; public int maxDistance;

private Transform myTransform;

void Awake() {
    myTransform = transform;
}

// Use this for initialization
void Start () {
    GameObject go = GameObject.FindGameObjectWithTag("Player");

    target = go.transform;

    maxDistance = 1;   
}

// Update is called once per frame
void Update () {
    Debug.DrawLine(target.position, myTransform.position, Color.yellow);

    //Look at target
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

    if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
       //Move towards target
       myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    }
}

}

Any Ideas :)?

more ▼

asked Apr 22 '12 at 04:16 PM

Nahoyman78 gravatar image

Nahoyman78
35 6 12 13

What is the specific problem: What does it do now and how do you want that to change?

This looks like it should move closer until it gets to 1 unit away, which seems fine (but it measures center to center, so if you used normal sized cubes, 1 unit away is touching.)

Apr 22 '12 at 04:21 PM Owen Reynolds

I want to make so If a Player Touches/Walk near the Enemy. It starts to Follow it until the enemy / player dies

Apr 22 '12 at 04:25 PM Nahoyman78

Any Ideas? :)

Apr 22 '12 at 04:34 PM Nahoyman78
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Ok, this would be a simple version. You have your target (player) that you drag in the inspector. Then it compares the position of each and if smaller than a range the NPC looks at the player and walk towards him. If the NPC gets too close, it stops walking. This way your guy is keeping its distance from the player and just follows him around.

var target :Transform;
var maxRange:int;
var minRange:int;

if((Vector3.Distance(transform.position,target.position)<maxRange)
   && (Vector3.Distance(transform.position,target.position)>minRange)){
      transform.LookAt(target);
      transform.Translate(Vector3.forward * Time.deltaTime);
}

Now if you want the NPC to follow no matter what

var target :Transform;
var minRange:int;
var follow:boolean;

function Update(){
if(Vector3.Distance(transform.position,target.position)<minRange)
   follow=true;
if(follow){
   transform.LookAt(target);
   transform.Translate(Vector3.forward * Time.deltaTime);}
}

Now your guy will start follow when you get close and since the boolean never changes it will follow you till death.

more ▼

answered Apr 22 '12 at 05:14 PM

fafase gravatar image

fafase
11.1k 11 15 45

thanks buddy :)

Apr 22 '12 at 05:14 PM Nahoyman78

but i get a Error:

Assets/Scripts/AI Follow.js(7,13): BCE0005: Unknown identifier: 'Distance'.

Apr 22 '12 at 05:17 PM Nahoyman78

Ooops Vector3.Distance()

Apr 22 '12 at 05:19 PM fafase

And it is JS, you will have to convert...

Apr 22 '12 at 05:20 PM fafase

Yeah. Thanks :)

Apr 22 '12 at 05:23 PM Nahoyman78
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x983
x832
x337
x319
x35

asked: Apr 22 '12 at 04:16 PM

Seen: 1590 times

Last Updated: Jun 05 at 12:19 AM