Itay080
Itay080

Reputation: 172

Unity|Moving gameobjects away from other gameobjects which are moving towards them

I have 2 AI game objects which are both capsules. Considering the first capsule AI is named X and the second capsule AI is named Y, I try to make Y move away from X (escape from him) while X is chasing Y (following him). I have no idea how to do that, I would appreciate a direction.

I tried to do what's written here, but they both move through walls even though they have capsule collider, I tried to do this:

http://forum.unity3d.com/threads/getting-objects-to-move-away-from-my-users-gameobject.142468/

but they only move in one direction and through walls.

Vector3 position = transform.position;
Vector3 targetPosition = target.transform.position;
Vector3 direction = position - targetPosition;
transform.position += direction * 2.0f * Time.deltaTime;`

Upvotes: 1

Views: 2658

Answers (1)

Everts
Everts

Reputation: 10701

You are moving them with transform.Translate. Moving the transform means "Put the object at the given position regardless of environment". If you want them to interact, you need to either use the CharacterController component and its methods or a rigidbody component and move it with force.

Upvotes: 1

Related Questions