Ok i have multiple enemies and multiple players in my scene. My problem was that when i an enemy killed a player they would stop searching for players tagged “playerguy”. So i tried changing GameObject.FindWithTag(“PlayerGuy”).Transform to GameObject.FindGameObjectsWithTag(“PlayerGuy”).Transform. Problem is i get this error
`Assets/TankMan/TankManScripts/EnemyScripts/Lvl1TerroristAI.js(29,74):
BCE0019: ‘transform’ is not a member
of ‘UnityEngine.GameObject’.
I understand why im getting this error, I just dont know how to fix it. If anyone can point me in the direction of fixing the problem that would be great.
static var target1 : Transform;
var moveSpeed : int = 6; // chase speed
var rotationSpeed : int = 1; // speed to turn to the player
var maxDistance : int = 10; // attack distance
var minDistance : int = 15; // detection distance
private var myTransform : Transform;
function Awake() {
myTransform = transform;
}
// Cache the controller
private var characterController : CharacterController;
characterController = GetComponent(CharacterController);
function Start () {
target1 = GameObject.FindWithTag ("PlayerGuy").transform;
maxDistance = 2;
}
//------------------------------------------------------------------
function Update(){
if (target1 == null){
target1 = GameObject.FindGameObjectsWithTag("PlayerGuy").transform;
if (target1) {
var dist = Vector3.Distance(target1.position, transform.position);
if (dist > minDistance){ // if dist > minDistance: enters idle mode
Idle();
}
else
if (dist <= maxDistance) { // if dist <= maxDistance: stop and attack
print ("Attack!");
}
else { // if maxDistance < dist < minDistance: chase the player
// print ("I found you "+ dist);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position) , rotationSpeed * Time.deltaTime);
// Move towards target
characterController.Move(myTransform.forward * moveSpeed * Time.deltaTime);
//
//
}
if (dist <= maxDistance){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position) , rotationSpeed * Time.deltaTime);
}
}
}
}
//------------------------------------------------------------------
var walkSpeed = 3.0;
var directionTraveltime = 2.0;
var idleTime = 1.5;
var rndAngle = 45;
// enemy will turn +/- rndAngle
private var timeToNewDirection = 0.0;
private var turningTime = 0.0;
private var turn: float;
function Idle () {
// Walk around and pause in random directions unless the player is within range
if (Time.time > timeToNewDirection) {
// time to change direction?
if(Random.value > 0.5) // choose new direction
turn = rndAngle;
else { turn = -rndAngle; }
turningTime = Time.time + idleTime;
// will stop and turn during idleTime...
timeToNewDirection = turningTime + directionTraveltime;
// and travel during directionTraveltime
}
if (Time.time < turningTime) {
// rotate during idleTime...
transform.Rotate(0, turn*Time.deltaTime/idleTime, 0);
} else {
// and travel until timeToNewDirection
characterController.SimpleMove(transform.forward * walkSpeed);
}
}
function OnCollisionEnter(collision : Collision) {
transform.Rotate(0, turn*Time.deltaTime/idleTime, 0);
}