x


Any ideas on a simple AI script that doesn't use any drag/drop variables

So I have a spawn script that uses instantiate to spawn an enemy prefab. However the AI script on the enemy uses the position of the player within the hierarchy and instantiated prefabs do not keep these variables. does anybody have any advice on how to create an ai script that doesn't use this but still moves towards the player?

more ▼

asked Jan 31, 2012 at 01:54 PM

simonheartscake gravatar image

simonheartscake
1 31 920 29

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If your player object has a tag, you can use

 GameObject.FindWithTag(tagName);

to get the first found object with that tag. If you are using the "Player" tag, and make sure that there is only ever one object with it, it should work fine.

more ▼

answered Jan 31, 2012 at 01:56 PM

syclamoth gravatar image

syclamoth
18.5k 20 28 121

Thanks alot, so you think like

var player : GameObject; var playerPosition : Vector3;

function Update (){

player = GameObject.FindWithTag(Player); playerPosition = player.transform.position;

?

Jan 31, 2012 at 05:00 PM simonheartscake

This worked great. Thanks

Jan 31, 2012 at 06:06 PM simonheartscake
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2953
x2150
x1402
x1401

asked: Jan 31, 2012 at 01:54 PM

Seen: 991 times

Last Updated: Jan 31, 2012 at 06:06 PM