- Home /
NPC will walk through... EVERYTHING to get to player
Have got a sneaking suspicion I know the answer to this, but thought I'd post the question to confirm / find another way:
I have an NPC with an AI script attached which directs him to the player. There is a table between the NPC and the player. Despite any type of colliders or rigibody that I place on the table, the NPC will walk straight through it, as if it wasn't even there. The player is stopped by the collider on the table, and the npc has their own collider. Based on my searching it seems like the issue might be because I'm using transform.position to move the npc, thus ignoring any colliders. Can anyone confirm this? Also, I also see one recommended fix is to use "simplemove," but I haven't used that before. (am reading up on it via Unity Script Reference) The question is, would that be the way to go, and if anyone is kind enough (I know this is asking alot) how would I implement it instead of transform.position? Thanks for your patient help, and God bless.
var target : Transform;
var moveSpeed = 1;
var rotationSpeed = 2;
var myTransform : Transform;
var minDistance = 30;
var stopDistance = 4;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("PlayerReferencePoint").transform;
}
function Update () {
if(Vector3.Distance(myTransform.position, target.position) < minDistance)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
Halt();
}
}
function Halt()
{
if(Vector3.Distance(myTransform.position, target.position) < stopDistance)
moveSpeed = 0;
else{
moveSpeed = 1;
}
}
second ai script, if necessary:
var target : Transform;
var moveSpeed = 5;
var rotationSpeed = 5;
var myTransform : Transform;
var $$anonymous$$Distance = 5;
var follow : boolean = true;
var damage = 8;
var stopDistance = 4;
function Awake(){
myTransform = transform;
}
function Start(){
target = GameObject.FindWithTag("PlayerReferencePoint").transform;
}
function Update () {
if(Vector3.Distance(myTransform.position, target.position) > stopDistance){
animation.Play("BDGuardWalk");
}
else{
if(Vector3.Distance(myTransform.position, target.position) <= stopDistance)
{
animation.Play("BDGuardAttack");
Halt();
}
}
}
function OnTriggerEnter (col : Collider) {
if(col.gameObject.tag == "Player") {
Playerhealth.curHealth -= damage;
}
}
function Halt(){
if(Vector3.Distance(myTransform.position, target.position) < stopDistance)
moveSpeed = 0;
else{
moveSpeed = 1;
}