Monster AI Discovery Player (visual range Discovery series)

Source: Internet
Author: User

Find some information on the Internet and write your own ideas.

Thank Momo and other great gods here.

We use the Player detection method to detect monsters, so compare trial and weak network game, each time when synchronizing players to determine the position of the player and the monster.

Here are two ways of handling:

1. Collider r range Detection.

2. Map chunking range detection.

Both of these treatments apply to different games.

Before we go into these two ways, let's take a look at a core question, how to tell if a monster is within the player's visual range when it's within the player's R radius.

Such as

In the diagram,

The position of the player and the location of the monster will be a vector. Set to V1

The direction of the monster is also a vector. Set to V2

If the monster can see the 60° angle. Then the vector (V1,V2), if less than 30 °, proves that the player is within the visual range of the monster.

The code is:

    Private BOOLFind (Gameobject _monster) {Otherpos=_monster.gameobject.transform.position; V= Transform.position-Otherpos; V.y=0.5fand//Handle the y-axis, because the judgment is based on the two-dimensional w= _monster.gameobject.getcomponent<findplayerai> (). Getface ()-Otherpos; W.y=0.5f; if(Vector3.angle (V, W) < -)        {            return true; }        return false; }

PS: If you want to judge on three-dimensional, the same way you can handle the YZ axis plane is OK.

Now let's take a look at the first way of handling:

This is relatively simple and very brain-free. It's good to add a spherecollider to the player, and you can also judge the three-dimensional state. Note: If terrain is used for ground use, block.

Collider Code

    void Ontriggerenter (Collider other)    {        if (!other.name.equals ("Terrain"))        {            if  (Find (Other.gameobject))//Connect to a Find code            {                Debug.Log (" discovery ") );            }        }    }

Then there is the second way to compare ideas.

Tiles the game map. Such as

Here it is. The general idea is:

Players in block No. 0, in this state, only a B monster can be viewed with the player angle, cdef are ignored.

The code is as follows:

Map.cs:

 Public classmap:monobehaviour{Private StaticMap _instance;  Public StaticMap Instance {Get        {            if(_instance = =NULL) {_instance=NewGameobject ("_map"). Addcomponent<map>(); }            return_instance; }    }    Private intPlayerpos =-1; Privategameobject[][] Monster =Newgameobject[ -][]; Private int[] Monsternum =New int[ -]; //public int n = 0;    voidAwake () { for(inti =0; I < -; i++) {Monster[i]=Newgameobject[Ten]; Monsternum[i]=0; }    }    intGetPos (Gameobject go) {floatx =go.transform.position.x; floatz =go.transform.position.z; X/= -; Z/= -; intpos = (int) (Mathf.floor (x) + Mathf.floor (z) *Ten); returnPOS; }     Public voidSetmonsterpos (Gameobject _monster) {intpos =GetPos (_monster); Monster[pos][monsternum[pos]++] =_monster; Debug.Log (POS+_monster.name); }         Public intGetplayerpos (Gameobject _player) {intpos =GetPos (_player); if(Playerpos = =POS) {            return-1; }        Else{Playerpos=POS; }        returnPOS; }     PublicGameobject[] Getmonster (intPOS) {        returnMonster[pos]; }}

Player AI.cs:

 Public classPlayerai:monobehaviour {Vector3 v;    Vector3 W;    Vector3 Otherpos; Gameobject[] Monster=Newgameobject[Ten]; Private BOOLFind (Gameobject _monster) {Otherpos=_monster.gameobject.transform.position; V= Transform.position-Otherpos; V.y=0.5f; W= _monster.gameobject.getcomponent<monsterai> (). Getface ()-Otherpos; W.y=0.5f; if(Vector3.angle (V, W) < -)        {            return true; }        return false; }    //void Ontriggerenter (Collider other)//{    //if (!other.name.equals ("Terrain"))//    {    //if (Find (other.gameobject))//        {    //Debug.Log ("discovery"); //        }    //    }    //}    voidUpdate () {intpos =Map.Instance.getPlayerPos (Gameobject); if(POS! =-1) {Monster=Map.Instance.getMonster (POS); }         for(inti =0; i < monster.length; i++)        {            if(Monster[i]! =NULL)            {                if(Find (Monster[i])) {Debug.Log (i+"found"); }            }        }    }}

Monster AI.cs:

 Public classMonsterai:monobehaviour { PublicGameobject Player; PrivateVector3 face; voidStart () { face= transform.position + transform.rotation * Vector3.forward *Ten;    Map.Instance.setMonsterPos (Gameobject); }     PublicVector3 Getface () {returnFace ; }    voidUpdate () { face= transform.position + transform.rotation * Vector3.forward *Ten; //Debug.drawline (Transform.position, Face, color.red);    }}

Monster AI Discovery Player (visual range Discovery series)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.