Hello,
I have a Strange thing going on and need a solution if anyone knows?
I have a Enemy who’s AI “Looks At” my Players Transform which is a variable in the Inspector.
If I drag and drop my Player from the Scene View onto the Player Variable (for LookAt) everything works fine and the enemy is always going after my Player.
Heres the Problem:
If I instantiate the Enemy at runtime it wont remember or hold my players Transform, So Im thinking I need to add its Prefab.
If I drag my Players Prefab onto the var Player (for Look) At the Enemy will go to where the players prefab was and not go after my player???
I need use the Players Prefab because when I instantiate the Enemy its the only way for it to Hold My Players Transform.
Ahh… this is confusing anyone ever run into this problem? and would anyone know how I would solve this?
myCode:
var awareDistance : float = 15.0;
var scaredDistance : float = 10.0;
var player : Transform;
var runSpeed : float = 4.0;
enum AIStatus {Idle = 0, Scared = 1}
private var status = AIStatus.Idle; //by default
var controller : CharacterController;
private var moveDirection = Vector3.zero;
//function Start loop animations
function Start()
{
//set all animations to loop
animation.wrapMode = WrapMode.Loop;
}
function Awake()
{
//maybe replace character controller with box collider rigidBody for gravity
controller = GetComponent(CharacterController);
}
function Update()
{
CheckStatus();
switch(status)
{
case AIStatus.Idle:
Idle();
break;
case AIStatus.Scared:
RunAway();
break;
}
}
function Idle()
{
animation.CrossFade ("idle2");
}
function RunAway()
{
//transform.eulerAngles.y = -player.transform.eulerAngles.y; //gets thier rotation from mine
// give enemy thier own rotation
transform.LookAt(player.transform);
//to make them run the other way add 180 degrees
//transform.eulerAngles.y += 180;
moveDirection = Vector3(0,0,40); //help our robot know hich way to go create a float variable to test it.
moveDirection = transform.TransformDirection(moveDirection); //converst it to worldSpace
moveDirection *= runSpeed; //times 4.0 above
controller.SimpleMove(moveDirection * Time.deltaTime); // moves our enemy
animation.CrossFade ("relax_walk1"); //and plays the run animation
}
function CheckStatus()
{
//player.position is what ever enemy I drag into the player varable above in the inspector
var dist = (player.position - transform.position).magnitude; //returns the length for dist
if (dist < scaredDistance)
{
status = AIStatus.Scared;
}
else if (dist > awareDistance)
{
status = AIStatus.Idle;
}
}