The protagonist of the Unity3d Institute faces toward the direction of the object in a certain area (45)

Source: Internet
Author: User
Tags abs
the protagonist of the Unity3d Institute is facing toward the direction of the object in a certain area (45) .

2013-3-4 13:30| Published by: chino| View: 1263| Comments: 1| Original Author: MOMO Abstract: Written in front of the words, two days ago a friend on the QQ asked me how to get the protagonist facing the direction of a certain area of the enemy objects. This proposition seems simple, in fact, contains a lot of mathematical aspects of things. Today I just have time to write this question in my blog. Hope can help him. In...

Written in front of the words, two days ago a friend on QQ asked me how to get the protagonist facing the direction of a certain area of the enemy objects. This proposition seems simple, in fact, contains a lot of mathematical aspects of things. Today I just have time to write this question in my blog. Hope can help him.

Before the code, please let me do a few simple exercises, angle vector calculation must learn, otherwise the things will be difficult to understand.

1. Known 3D coordinates, and a rotation angle, and a distance, to find the target point of 3D coordinates.

The current point is known as Target, the target point rotates 30 degrees along the y axis of target, and 10 meters along the x axis of the target to find the 3D coordinates of the destination point.

[code]:

Using Unityengine;
Using System.Collections;

public class Test:monobehaviour
{public

	Transform Target;

	void Lateupdate ()
	{
		quaternion rotation = Quaternion.euler (0f,30f,0f) * target.rotation;
		Vector3  newpos = rotation * New Vector3 (10f,0f,0f);
		Debug.drawline (newpos,vector3.zero,color.red);
		Debug.Log ("Newpos" + Newpos + "Nowpos" + target.position + "Distance" + vector3.distance (newpos,target.position)); 
      }

}

Output: New coordinates (8.7, 0.0,-5.0) The distance 0.0 between the current coordinates (0.0, 0.0, two) 10 points.

2. The vector of the 3D model is known for its angle.

It is known that the 3D model Target,y axis rotates 30 degrees and then pans forward.

[code]:

Using Unityengine;
Using System.Collections;

public class Test:monobehaviour
{public

	Transform Target;

	void Lateupdate ()
	{

		if (Input.getmousebutton (0))
		{
			quaternion rotation = Quaternion.euler (0f,30f,0f ) * target.rotation;
			Vector3  newpos = rotation * Vector3.forward;
			Target.translate (NEWPOS.X,NEWPOS.Y,NEWPOS.Z);}}}

3. A target point is known to move the model toward this target point.

This is a relatively simple example, we should all be able to understand.

[code]:

Target.transform.LookAt (New Vector3 (100f,200f,300f));
		Target.translate (Vector3.forward);

What I want to say here is Vector3.forward, which is equivalent to new Vector3 (0,0,1); it is not a coordinate, it is a standard vector, and the direction is forward along the z axis. So the distance to pan once is 1 meters, if Vector3.forward * 100 then the distance of the translation is 100 meters.

Look at the code below.

[code]:

Vector3 VECN = (targetcube.position-target.position). normalized;

		Target.translate (VECN *0.1f);

Subtract a vector from a vector to find their difference, normalized is a format vector, which means to format the vectors between them within 1 meters. This allows for a more accurate calculation of the distance of a translation VECN *0.1f is marked with a pan of 1 centimeters, clams.

The vector can be rotated not only by moving the X y Z axis, but by letting the vector rotate 30 degrees along the Y axis.

[code]:

Vector3 VECN = (targetcube.position-target.position). normalized;

	VECN = Quaternion.euler (0f,30f,0f) * VECN;

	Target.translate (VECN *0.1f);

If the above three simple exercises you can know the heart of the words, then the biggest problem of this article I believe it will not be any difficult, continue to read it.

Suppose we need to calculate all the objects in front of the protagonist in 5 meters. The center of the protagonist to calculate the front of a point 5 meters away, in order to let everyone see clearly I now draw this line.

[code]:

private float distance = 5f;
void Update () 
{
	quaternion r= transform.rotation;
	Vector3 F0 =  (transform.position  + (r *vector3.forward) * distance);
	Debug.drawline (transform.position,f0,color.red);
}

As shown in the following diagram, we have calculated these two points. At this point you can dynamically edit the coordinates of the protagonist's Y axis, which is always a point 5 meters away from the current angle of the protagonist.

Next, we need to calculate a divergence angle in front of the protagonist. Suppose the protagonist sees the left 30 degrees to the right and 30 degrees in this area.

[code]:

private float distance = 5f;
void Update () 
{
	quaternion r= transform.rotation;
	Vector3 F0 =  (transform.position  + (r *vector3.forward) * distance);
	Debug.drawline (transform.position,f0,color.red);

	quaternion r0= Quaternion.euler (transform.rotation.eulerangles.x,transform.rotation.eulerangles.y-30f, TRANSFORM.ROTATION.EULERANGLES.Z);
	quaternion r1= Quaternion.euler (transform.rotation.eulerangles.x,transform.rotation.eulerangles.y + 30f, TRANSFORM.ROTATION.EULERANGLES.Z);

	Vector3 f1 =  (transform.position  + (r0 *vector3.forward) * distance);
	Vector3 F2 =  (transform.position  + (r1 *vector3.forward) * distance);

	Debug.drawline (transform.position,f1,color.red);
	Debug.drawline (transform.position,f2,color.red);

	Debug.drawline (f0,f1,color.red);
	Debug.drawline (f0,f2,color.red);
}

As shown in the figure below, the area before the protagonist is calculated. Looks like an area between two triangles.

The last is a simple formula to calculate whether a point is within the triangle, in this article is to calculate whether the enemy points in front of the two triangles.

[code]:

Using Unityengine;

Using System.Collections;

	public class Mytest:monobehaviour {public Transform cube;
	private float distance = 5f;
		void Update () {quaternion r= transform.rotation;
		Vector3 F0 = (transform.position + (R *vector3.forward) * distance);

		Debug.drawline (transform.position,f0,color.red); quaternion r0= Quaternion.euler (transform.rotation.eulerangles.x,transform.rotation.eulerangles.y-30f,
		TRANSFORM.ROTATION.EULERANGLES.Z); quaternion r1= Quaternion.euler (transform.rotation.eulerangles.x,transform.rotation.eulerangles.y + 30f,

		TRANSFORM.ROTATION.EULERANGLES.Z);
		Vector3 F1 = (transform.position + (R0 *vector3.forward) * distance);

		Vector3 F2 = (transform.position + (r1 *vector3.forward) * distance);
		Debug.drawline (transform.position,f1,color.red);

		Debug.drawline (transform.position,f2,color.red);
		Debug.drawline (f0,f1,color.red);

		Debug.drawline (f0,f2,color.red);

		Vector3 point = cube.position; if (Isintriangle (point,transform.positION,F1,F0) | |
		Isintriangle (point,transform.position,f2,f0)) {Debug.Log ("cube in this!!!");
		}else {Debug.Log ("cube not in this!!!"); }} private float Trianglearea (float v0x,float v0y,float v1x,float v1y,float v2x,float v2y) {return MATHF.
    Abs ((v0x * v1y + v1x * v2y + v2x * v0y-v1x * v0y-v2x * v1y-v0x * v2y)/2f);
		} bool Isintriangle (Vector3 Point,vector3 V0,vector3 V1,vector3 v2) {float x = point.x;

		Float y = point.z;
		float v0x = v0.x;

		float v0y = v0.z;
		float v1x = v1.x;

		float v1y = v1.z;
		float V2X = v2.x;

		float v2y = v2.z;
		float T = trianglearea (V0X,V0Y,V1X,V1Y,V2X,V2Y);

		float a = Trianglearea (v0x,v0y,v1x,v1y,x,y) + Trianglearea (v0x,v0y,x,y,v2x,v2y) + Trianglearea (x,y,v1x,v1y,v2x,v2y);
		if (Mathf.abs (t-a) <= 0.01f) {return true;
		}else {return false; }
	}
}

As shown in the figure below, if the box object is in the protagonist's field of view, it will be detected.

Note that I have selected two triangles in my field of view above, and if you want the target point of the field of view to be elliptical, you can set up some more triangles. But this will be very efficient, I think here can use 1 triangles, only the right target point will have some deviation, the effect is not very large. As shown in the figure below

The code can be easily modified.

[code]:

Using Unityengine;

Using System.Collections;

	public class Mytest:monobehaviour {public Transform cube;
	private float distance = 5f;
		void Update () {quaternion r= transform.rotation;
		Vector3 F0 = (transform.position + (R *vector3.forward) * distance);

		Debug.drawline (transform.position,f0,color.red); quaternion r0= Quaternion.euler (transform.rotation.eulerangles.x,transform.rotation.eulerangles.y-30f,
		TRANSFORM.ROTATION.EULERANGLES.Z); quaternion r1= Quaternion.euler (transform.rotation.eulerangles.x,transform.rotation.eulerangles.y + 30f,

		TRANSFORM.ROTATION.EULERANGLES.Z);
		Vector3 F1 = (transform.position + (R0 *vector3.forward) * distance);

		Vector3 F2 = (transform.position + (r1 *vector3.forward) * distance);
		Debug.drawline (transform.position,f1,color.red);
		Debug.drawline (transform.position,f2,color.red);

		Debug.drawline (f1,f2,color.red);

		Vector3 point = cube.position; if (Isintriangle (POINT,TRANSFORM.POSITION,F1,F2)) {Debug.Log ("cube in this!!! ");}
		else {Debug.Log ("Cube not!!!"); }} private float Trianglearea (float v0x,float v0y,float v1x,float v1y,float v2x,float v2y) {return MATHF.
    Abs ((v0x * v1y + v1x * v2y + v2x * v0y-v1x * v0y-v2x * v1y-v0x * v2y)/2f);
		} bool Isintriangle (Vector3 Point,vector3 V0,vector3 V1,vector3 v2) {float x = point.x;

		Float y = point.z;
		float v0x = v0.x;

		float v0y = v0.z;
		float v1x = v1.x;

		float v1y = v1.z;
		float V2X = v2.x;

		float v2y = v2.z;
		float T = trianglearea (V0X,V0Y,V1X,V1Y,V2X,V2Y);

		float a = Trianglearea (v0x,v0y,v1x,v1y,x,y) + Trianglearea (v0x,v0y,x,y,v2x,v2y) + Trianglearea (x,y,v1x,v1y,v2x,v2y);
		if (Mathf.abs (t-a) <= 0.01f) {return true;
		}else {return false; }
	}
}

Above we introduce the triangle judgment, of course, you can also use the rectangle to determine whether the intersection.

Code:

[code]:

Using Unityengine;

Using System.Collections;

	public class Mytest:monobehaviour {public Transform cube;
	private float distance = 5f;
		void Update () {quaternion r= transform.rotation;
		Vector3 left = (transform.position + (R *vector3.left) * distance);

		Debug.drawline (transform.position,left,color.red);
		Vector3 right = (transform.position + (R *vector3.right) * distance);

		Debug.drawline (transform.position,right,color.red);
		Vector3 Leftend = (left + (r *vector3.forward) * distance);

		Debug.drawline (left,leftend,color.red);
		Vector3 Rightend = (right + (R *vector3.forward) * distance);

		Debug.drawline (right,rightend,color.red);

		Debug.drawline (leftend,rightend,color.red);

		Vector3 point = cube.position;
		if (Isinrect (Point,leftend,rightend,right,left)) {Debug.Log ("cube in this!!!");
		}else {Debug.Log ("cube not in this!!!");
    }} private float Multiply (float p1x, float p1y, float p2x,float p2y, float p0x,float p0y) {    Return ((p1x-p0x) * (p2y-p0y)-(p2x-p0x) * (p1y-p0y));
		} bool Isinrect (Vector3 Point,vector3 V0,vector3 V1,vector3 V2,vector3 v3) {float x = point.x;

		Float y = point.z;
		float v0x = v0.x;

		float v0y = v0.z;
		float v1x = v1.x;

		float v1y = v1.z;
		float V2X = v2.x;

		float v2y = v2.z;
		float v3x = v3.x;

		float v3y = v3.z;  if (Multiply (x, Y, v0x,v0y, v1x,v1y) * Multiply (x, Y, v3x,v3y, v2x,v2y) <= 0 && Multiply (x, Y, v3x,v3y, v0x,v0y) *
		Multiply (x, Y, v2x,v2y, v1x,v1y) <= 0) return true;

	else return false; }

}

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.