x


stop enemies from walking into each other

well I have created this script and when the enemies target me they gather infront of me. I want them to keep distance

here is my code

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 = 2;   
}


void Update () {
    Debug.DrawLine(target.position, myTransform.position, Color.yellow);


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

    if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {

       myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    }
}

I think my problem is

myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

however I do not know how to fix it.

please help = )

more ▼

asked Jan 20 '12 at 03:25 AM

Babilinski gravatar image

Babilinski
84 40 42 43

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The easiest way is to add a CharacterController to the enemy and use Move to move it (or SimpleMove, if you want to include gravity): the CharacterController detects collisions with any collider, thus will not occupy the same place as the others.

public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;

private Transform myTransform;

void Awake() {
    myTransform = transform;
}

CharacterController character;

void Start () {
    GameObject go = GameObject.FindGameObjectWithTag("Player");
    target = go.transform;
    character = GetComponent<CharacterController>();
    maxDistance = 2;   
}

void Update () {
    Debug.DrawLine(target.position, myTransform.position, Color.yellow);
    Vector3 dir = target.position - myTransform.position;
    dir.y = 0; // kill height differences to avoid enemy tilting
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);
    if (dir.magnitude > maxDistance) {
        character.Move(myTransform.forward * moveSpeed * Time.deltaTime);
    }
}

If you want to include gravity, replace character.Move with:

    character.SimpleMove(myTransform.forward * moveSpeed);
more ▼

answered Jan 20 '12 at 04:02 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

when I add character.SimpleMove(Vector3.forward * moveSpeed); he starts moving to the side

Jan 20 '12 at 10:20 PM Babilinski

Do you have other colliders childed to the character? The CharacterController has a bug that makes it move to weird directions when any childed collider is in touch with the character capsule collider. To solve this, mark Is Trigger in any childed object's collider (or just delete the child's collider).

Jan 20 '12 at 10:47 PM aldonaletto

Forget about the previous comment about the bug: I forgot to convert the direction to world space. Actually, it's easier to use transform.forward instead. I edited my answer to use world direction.
NOTE: Well, don't forget about the bug thing completely: it's important to know about this bug!

Jan 20 '12 at 10:54 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

Tried your script. The problem in not in the statement you think. In the start function you are keeping the value of maxDistance to 2 always when the script runs. Remove the statement " maxDistance = 2" from Start function and specify a value of maxDistance through the editor's inspector window. I put it to 7 and it stops at a larger distance now.

I Hope this helps

more ▼

answered Jan 20 '12 at 04:08 AM

vatsalAtFEI gravatar image

vatsalAtFEI
734 13 18 25

(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:

x4165
x2500
x243
x207

asked: Jan 20 '12 at 03:25 AM

Seen: 736 times

Last Updated: Jan 20 '12 at 10:55 PM