- Home /
the question was answered in the comments section
adding tag to the player causes issues to the npc
whenever I add a tag to my target it makes the npc accessing the script to start playing a particular animation automatically and it doesn't respond to any other condition in the script, I have tried using target= GameObject.Find("cougar")
but it still throws the same error and if I don't add a tag it throws a null reference exception, please i've been battling with this for months now and any help will be appreciated here is the code below
function Awake()
{
myTransform = transform; //cache transform data for easy access/performance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
target = GetComponent(Transform);
myTransform = GetComponent(Transform);
}
unction strotAway()
{
animation["deerStrot"].speed=4;
animation.CrossFade("deerStrot");
//rotate to look away from the player
myTransform.rotation = Quaternion.LookRotation(myTransform.position - target.transform.position);
// move away from player
moveDirection = myTransform.position-target.position;
myTransform.position += moveDirection*strotSpeed*Time.deltaTime;
}
When strotAway
is called?
There are a couple of issues with the code: lines 10-11. The line 10 makes the previous line pointless because you replace the target
value with the Transform
of your current object, not the player. And line 11 does the same as line 3 in Awake
so you can effectively remove it.
@moonstruck you are a life saver and I will never forget your help thanks
@duclaw can you mark @moonstruck's answer as correct please :D