Ok, so i have a simple AI Script, and i want to spawn enemies 5 seconds after the scene starts. I have that working, but the prefab i have can’t have things in the hierarchy as a target when in its prefab form. Heres a pic to show you.
Now, i have this script, but how do i change it so it can access hierarchy things?
var followSpeed: float;
var Player : Transform;
var rotationSpeed = 3; //speed of turning
function Awake(){
Test.enemysalive +=1;
}
function FixedUpdate() {
var cnt: CharacterController = GetComponent(CharacterController);
var direction = (Player.position - transform.position).normalized;
var movement = direction * followSpeed - Vector3.up;
cnt.Move(movement * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Player.position-transform.position), rotationSpeed*Time.deltaTime);
}
Accessing hierarchy things is no problem, you can just drag it into the reference there and then apply again so the prefab is updated (your image shows that you try to use something from project to assign thats no problem. you can’t drag anything thats in the scene hierarchy though on prefabs as only “fixed static objects” ie prefabs can be used on other prefabs)
You just can not have a hierarchy of prefabs. Only the root object will become a prefab.
If you need childs that are own prefabs, you need to make a transform / GameObject array in a MonoBehaviour to which you apply the “Root Prefabs”, to then instantiate them on Awake
But I’m not sure I understand your actual problem so if this wasn’t the question you might need to rephrase it
Well the target for the AI is my player, who is already in the scene. So, since the enemy is a prefab, it won’t let me set the player in the player variable on the enemy prefab.
Setting a prefab’s variable to point at another dynamic prefab (the player, if it’s a prefab) won’t work since the position won’t be updated. So Nikolay’s code samples are good solutions that I use too. If the player is there from the beginning, use Awake() since Find-functions tend to be heavy and it’s preferred to call them when the game loads rather than when the level itself loads.
Wow thanks. I was going through the documentation for a couple hours searching all the things i could think of with “Transform” in them and missed that? Thanks a lot!
OK, sorry vicenti. When i use this code, it’s saying Position is not a member of GameObject. Any reason?
var followSpeed : float;
var Player : GameObject;
var rotationSpeed = 3; //speed of turning
function Awake(){
Test.enemysalive +=1;
}
function FixedUpdate() {
var cnt: CharacterController = GetComponent(CharacterController);
var direction = (Player.position - Player.transform.position).normalized;
var movement = direction * followSpeed - Vector3.up;
cnt.Move(movement * Time.deltaTime);
Player.transform.rotation = Quaternion.Slerp(Player.transform.rotation, Quaternion.LookRotation(Player.position-Player.transform.position), rotationSpeed*Time.deltaTime);
}
Transforms have a .position member, GameObject has a .transform member. Since Player is a GameObject, you need to use Player.transform.position.
[Unity - Scripting API: GameObject]GameObject[/url] - all MonoBehavoiurs (GameObject, Transform, Rigidbody, etc) inherit the properties gameObject, transform, rigidbody, collider, etcetera, so from any MonoBehaviour you can instantly access those basic components.
var player : GameObject;
x = player.transform.position; // okay
x = player.gameObject.transform.position; // weird, but it works
x = player.position // not okay, GameObject doesn't have a .position. You need to go through .transform first.
OK, so i got the enemy working, And if you don’t mind, how can i make my enemy only follow my enemy without going into the air? he just kind of flys around at the moment. Heres my code.
var followSpeed: float;
var Player;
var rotationSpeed = 3; //speed of
var enemy;
function Awake(){
Test.enemysalive +=1;
Player = GameObject.FindWithTag("Player");
enemy = this.gameObject;
}
function FixedUpdate() {
var cnt: CharacterController = GetComponent(CharacterController);
var direction = (Player.transform.position - enemy.transform.position).normalized;
var movement = direction * followSpeed - Vector3.up;
cnt.Move(movement * Time.deltaTime);
enemy.transform.rotation = Quaternion.Slerp(enemy.transform.rotation, Quaternion.LookRotation(Player.transform.position-enemy.transform.position), rotationSpeed*Time.deltaTime);
}