- Home /
Detect and react to collisions
hello. how can I make my npc react to a collision? I have an NPC with a charactercontroller and animation. it also has a script that makes he wandering inside a circle. but he does not react to obstacles through walls, etc.. how can I fix this?
Answer by Rett_ · Mar 02, 2013 at 10:22 PM
Make sure you are using CharacterController.Move instead of dealing directly with Transform. It will take care of basic collisions. If you want it to move around the obstacle, then you will need some kind of pathfinding algoritm (have a look)
I hadn't thought about that. in my wandering algorithm I have this code:
function $$anonymous$$oveTowardTarget():void{
var direction : Vector3 = currentTargetPosition - transform.position;
direction.y = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
if(direction.magnitude > $$anonymous$$Distance + 1){
var moveVector : Vector3 = direction.normalized * moveSpeed * Time.deltaTime;
transform.position += moveVector;
}
else {
FindNewTargetPosition();
currentState = State.Waiting;
}
}
for to use CharacterController.$$anonymous$$ove I must change transform.position += moveVector to myController.$$anonymous$$ove(moveVector)
perhaps.
thanks! ;)
Answer by highpockets · Mar 02, 2013 at 10:22 PM
I believe you will need to attach a kinematic rigid body to your NPC and handle it through OnTrigger.
Your answer
Follow this Question
Related Questions
when i collide with npc he keeps walking. 1 Answer
Terrain collision problem 1 Answer
Collision Enter one of the objects ? 1 Answer