- Home /
Enemys dont collide in follow script
As i said above, my enemy's are programmed to only follow the player, but if more than one is following me, they go inside each other. I want them to stay apart and follow me separate, can anyone tell me what I am doing wrong?
He has colliders on every body part. he is also designed to slow down if close to you so that he can attack, but the attacking has not been implemented yet.
var target : Transform;
var speed = .5;
var rotationSpeed = .2;
var actualSpeed;
function Start()
{
actualSpeed = .5;
}
function Update()
{
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(target.position - transform.position), rotationSpeed * Time.deltaTime);
transform.localEulerAngles = Vector3(0, transform.localEulerAngles.y, 0);
transform.position = Vector3.MoveTowards(transform.position, target.position, actualSpeed*Time.deltaTime);
transform.position.y = .1;
var distance = Vector3.Distance(target.transform.position, transform.position);
if (distance <= 3)
{
actualSpeed = speed / 2;
}
if (distance > 3)
{
actualSpeed = speed * 2;
}
if (Input.GetKeyDown ("space"))
{
//GetComponent("enemySlow").enabled = false;
}
}
Answer by Karsnen_2 · Jan 08, 2013 at 12:45 AM
When two collider intersect each other and stay in-tight then they must be marked as TRIGGERS. First check it.
Next for the NPC AI not to collide each other, you need to write a script which would say to slow if it is close to another collider of a similar tag. Lets say that NPC are scattered and they follow the player in a linear manner from their position. Upon converging near to the Player they might coerce. In such cases, use Raycasting.
Raycast a ray from the NPC to find for Obstacles with which you do not want to collide with. If it returns true, just decrease the speed depending upon the distance but do not null your speed.
I hope it helps. Let me know if you need more.
Cheers!!!