x


2 things...

A few things actually...

1st: i want obj_106(main enemy) to teleport close to the player but not at.

2nd: on attack. randomly Choose to either kill or teleport away?

more ▼

asked Jul 10 '12 at 07:36 AM

saitoFOX gravatar image

saitoFOX
21 1 4 10

Downvote:

  • Bad title, not a question, not descriptive
  • Way to general question without any information (2D / 3D, platform, language, type of game)
Jul 10 '12 at 03:03 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Usually I'm not much for answering these loose type of questions, we don't know what your world looks like and what the rules for the game world is. You haven't given us anything to work with actually to help you out.

The easiest way to handle AI is to use enums of their behavior. Here's an example that should fit your needs (put this on obj_106):

#pragma strict

enum TELEPORT {
 ToPlayer,
 AwayFromPlayer
}

var player : Transform;
private var myTransform : Transform;

function Start () {
 myTransform = transform;
 if (player==null) player=GameObject.FindGameObjectWithTag("Player").transform;
 InvokeRepeating("CycleExample", 2.0, 2.0);
}

//This function shows how to init the repositioning
private var exampleBool : boolean = false;
function CycleExample () {
 if (exampleBool) {
 Teleport(TELEPORT.ToPlayer);
 } else {
 Teleport(TELEPORT.AwayFromPlayer);
 }
 exampleBool=!exampleBool;
}

function Teleport (tel : TELEPORT) {
 switch (tel) {
 case TELEPORT.ToPlayer:
 myTransform.position = Reposition(.5);
 if (Random.Range(0,2)==1) {
 //Attack the player (50% chance)
 yield WaitForSeconds(Random.Range(0.5, 1.0));
 Attack();
 } else {
 //Dissapear (50% chance)
 yield WaitForSeconds(Random.Range(0.5, 1.0));
 Flee();
 }
 break;
 case TELEPORT.AwayFromPlayer:
 myTransform.position = Reposition(20.0);
 break;
 }
}

function Reposition (range : float) : Vector3 {
 Debug.Log(range);
 var angle : float = Random.Range(.0,180.0);
 var newPos : Vector3 = Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * range;
 return newPos;
}

function Attack () {
 //Play attack animation etc.
}

function Flee () {
 //Play flee animation etc.
 Teleport(TELEPORT.AwayFromPlayer);
}

The most important function here is the Reposition() which takes a range and returns a Vector3. This doesn't handle the positioning in Y-axis as you haven't given any information of the rules of your game. If this is a terrain then you could use a raycast in the downward direction from where newPos inside Reposition() is calculated.

Good luck!

more ▼

answered Jul 10 '12 at 09:04 AM

save gravatar image

save
8.2k 22 31 62

Is it just me who is clumsy or is the new UA having a bit of trouble with the indentation? Either way, sorry about that.

Jul 10 '12 at 09:09 AM save

thumbup but should clarify : I cannot speculate if ur clumsy (!) but changing to UDN has definitely stuffed up code formatting in answers and comments. I used to happily tab all my code, then paste and bam instant formatting. Now, its wearing out the spacebar on my keyboard ......

Jul 10 '12 at 09:13 AM alucardj

Erm... sorry... heres a better Discription of what i want 106 to do. and more detial...

Ehem. The world is in a forest/hilly environment outside of a the SCP foundation. When you go into 106's cage he uses the player to get out of his cell and transport the player into his pocket dimension... but his powers get messed up by some distress of being locked up for so long (i already got this part down dont worry) after being transported to this new area. not even known by 106, 106 trys to hunt you down and kill you. making fake 538's (shadow spiders got that part down aswell) (now heres the part i dont got) he slowly walks toward you at first entry of the game and teleports away. throughout time of the game he will teleport close to you and either choose to attack you removing half your health, or to scare you making (noise_11) and teleporting away for a bit.

here are my animations: walk Attack

his hp: Unkillable 2e+07

thank you >.< hope this is better then earlier.

Jul 10 '12 at 09:17 AM saitoFOX

@alucardj Haha yeah, hopefully it's fixed soon, imagine what all those minutes of spacing could be codewise. :-)

@saitoFOX Good thing! You'll get better help in the future if you also paste relevant code how far you've come. You have a good start there with the code given and I can't really develop the game for you. :-) I think you'll do fine coding this on your own!

Jul 10 '12 at 09:28 AM save

@save ... i coded most of the games forumlas myself o,.o im having problems with 2 things >.< please help... the code i have right now for 106 is just a Template AI behavior... so i have no idea what to do with him...

Jul 10 '12 at 09:31 AM saitoFOX
(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:

x155
x90

asked: Jul 10 '12 at 07:36 AM

Seen: 291 times

Last Updated: Jul 10 '12 at 03:03 PM