"Komatsu teaches you to develop" "Unity practical Skills" to calculate whether the target object is in its own fan field

Source: Internet
Author: User

Tag: no 51cto

In the game development often need to calculate the fan-shaped field of view or hit range.

In fact, this is divided into two parts,

The first part is within the fan range (i.e. regardless of the angle, in fact the circle range)

The second part is in the fan angle range

The first part is very simple, vector3.distance (A, b); calculating distances

Here is the second part, within the fan angle range.

Calculate if the monster is in your field of view, you can actually see this.

Whether the angle between the square vector of the Avatar and the Avatar to enemy is less than half the size of the line of sight.

This makes it possible to determine whether it is within sight range.

So the question now is how to calculate this angle.

The way to calculate is that

1. Get a point in the positive direction of the avatar and subtract from the avatar world coordinates, get the positive direction vector

2.Enemy's world coordinates are subtracted from the avatar world coordinates, obtaining a vector of directions enemy to Avatar

3. Call Vector3.angle (A, b) to calculate the angle

    public GameObject avatar;    public GameObject enemy;    float minDistance = 10f;    float minAngle = 120f;    // Update is called once per frame    void Update ()    {        Vector3 avatarPos = avatar.transform.position;        Vector3 enemyPos = enemy.transform.position;        float distance = Vector3.Distance(avatarPos, enemyPos);        //主角相对于目标的向量        Vector3 srcLocalVect = enemyPos - avatarPos;        srcLocalVect.y = 0;        //获取主角正前方的一个点        Vector3 forwardLocalPos = avatar.transform.forward * 1 + avatarPos;        //获取正方向向量        Vector3 forwardLocalVect = forwardLocalPos - avatarPos;        forwardLocalVect.y = 0;        //计算角度        float angle = Vector3.Angle(srcLocalVect, forwardLocalVect);        if(distance < minDistance && angle < minAngle/2)        {            Debug.LogError("In EyeSight");        }    }

"Komatsu teaches you to develop" "Unity practical Skills" to calculate whether the target object is in its own fan field

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.