- Home /
Question by
corax · Apr 14, 2012 at 06:59 PM ·
transformfindgameobjectswithtag
Store Transform game object with a tag
Hi guys,
I've wrote this basic IA. It works very well but know I want the script find automatically the player and store it in a variable. I've tagged the player with the default tag "Player" but I can't use the simple GameObject.FindWithTag("Player") because I've to access to transform properties. So any workaround? thanks
#pragma strict
var awerness = 15.0;
var scaryness = 10.0;
var runSpeed = 4.0;
var player : Transform;
var controller : CharacterController;
private var moveDirection = Vector3.zero;
enum AIStatus {Idle = 0, Scared = 1}
private var status = AIStatus.Idle;
function Awake (){
controller = GetComponent(CharacterController);
}
function Start () {
}
function Update () {
CheckStatus();
switch(status){
case AIStatus.Idle : Idle();
break;
case AIStatus.Scared: Runaway();
break;
}
}
function Idle(){
animation.CrossFade ("idle");
}
function Runaway(){
transform.eulerAngles.y = -player.transform.eulerAngles.y;
moveDirection = Vector3(0,0,40);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= runSpeed;
controller.SimpleMove(moveDirection * Time.deltaTime);
animation.CrossFade ("run");
}
function CheckStatus(){
var dist = (player.position - transform.position).magnitude;
if (dist < scaryness){
status = AIStatus.Scared;
}else{
status = AIStatus.Idle;
}
}
Comment
Answer by DaveA · Apr 14, 2012 at 07:02 PM
The GameObject returned by FindWithTag has a transform on it
var p = GameObject.FindWithTag("Player");
var t = p.transform;
Answer by yezzer · Apr 14, 2012 at 07:02 PM
Why can't you use:
GameObject.FindWithTag("Player").transform
That tip worked perfect for in C#.
Transform playerTarget = GameObject.FindWithTag("Player").transform;
Answer by gregzo · Apr 14, 2012 at 07:02 PM
Why not type var player as GameObject, and then access player.transform?