[Unity Combat] to determine if the character faces a certain area of the existence of objects

Source: Internet
Author: User

First is the mathematical knowledge that is greatly awakened by the rain Pine, code reference from here:


Using unityengine;using System.collections;public class Testrot:monobehaviour {public    Transform target;    void Start ()    {        //four rotation some operations        quaternion rotation = target.rotation * Quaternion.euler (0f, 30f, 0f);// The current angle rotates 30 degrees around the y-axis        Vector3 pos = rotation * New Vector3 (10f, 0f, 0f);//Current Angle forward 10 units along        the x-axis debug.drawline (POS, Vector3.zero, color.red);        Debug.Log ("Newpos" + pos + "Nowpos" + target.position + "Distance" + vector3.distance (POS, target.position));        The vector rotation        //current vector rotates 45 degrees around the y-axis, or it can be thought that the angle is rotated 45 degrees around the y-axis, moving 1 units along the z-axis        Vector3 a = Quaternion.euler (0f, 45f, 0f) * vector3.forward;//only rotates, the length of the vector is constant        Debug.Log (a);}    }

The original text is a method that determines whether an object is in a triangular area facing the character, but is more efficient, more accurate, and simpler:


Using unityengine;using System.collections;public class Testrot2:monobehaviour {public    Transform target;    public float angle = 60f;    public float distance = 5f;        void Update ()    {        Vector3 direction = target.position-transform.position;        if (Vector3.angle (direction, Transform.forward) < Angle)            if (Vector3.distance (Target.position, Transform.position) < distance)                print ("I've locked you up!" ");    }}


[Unity Combat] to determine if the character faces a certain area of the existence of objects

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.