Ray, clone, draw line, Ray clone draw line
1, Ray
(1) Ray rays
A ray is an infinite line starting at origin and going in some direction.
Ray is an infinite line with a starting point and a direction.
Structure:
Ray ray = new Ray(transform.position, transform.forward);
Transform. position is the starting point, and transform. forward is the direction.
(2) Raycast ray projection
C#
=> Public static bool Raycast (Vector3 sourcePosition, Vector3 targetPosition, out NavMeshHit hit, int areaMask );
SourcePosition: Ray start point
TargetPosition: Ray end point
Hit: Save the properties of the ray projection position
AreaMask: When drawing a path, the bitmask specifies the path that can be passed in the NavMesh area.
(3) RaycastHit ray projection Collision Information
BarycentricCoordinate |
The barycentric coordinate of the triangle we hit. Coordinates of the center of gravity of the triangle. |
Collider |
The Collider that was hit. Collision server. |
Distance |
The distance from the ray's origin to the impact point. The distance from the origin of the ray to the touch point. |
LightmapCoord |
The uv lightmap coordinate at the impact point. Coordinates of the UV light map at the touch point. |
Normal |
The normal of the surface the ray hit. Rays touch the normal of the surface. |
Point |
The impact point in world space where the ray hit the collider. In the world coordinate space, rays touch the contact point of the collision tool. |
Rigidbody |
The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null. The rigid body on the collision server. If no rigid body is attached to the collision generator, null is returned. |
TextureCoord |
The uv texture coordinate at the impact point. UV texture coordinates at the touch point. |
TextureCoord2 |
The secondary uv texture coordinate at the impact point. The second set of UV texture coordinates at the contact point. |
Transform |
The Transform of the rigidbody or collider that was hit. The transformation of the rigid body or collision. |
TriangleIndex |
The index of the triangle that was hit. The index of the triangle. |
2. Clone
C#
=> Static ObjectInstantiate(Object original, Vector3 position, Quaternion rotation );
C#
=> Static Object Instantiate (Object original );
Parameters
Original |
An existing object that you want to make a copy of. The existing object you want to copy |
Position |
Position for the new object. Location of the new object |
Rotation |
Orientation of the new object. Orientation of the new object |
Clones the object original and returns the clone. cl
Clone the original object and return the cloned object.
The above is taken from the Unity holy book.
3. Draw a line
Using UnityEngine; using System. collections; public class Drawwarnline: MonoBehaviour {// Use this for initialization private Vector3 pos1; // The Position of the first vertex private Vector3 pos2; // The position of the second vertex private RaycastHit hit; private Ray ray; private int num = 0; public bool ischoose; // determines whether to start drawing public GameObject warnline; // Line Model
Void Update () {if (Input. getMouseButtonDown (0) // press the left mouse button {ray = Camera. main. screenPointToRay (Input. mousePosition); // a ray is projected from the master camera to the mouse position if (Physics. raycast (ray, out hit) // cast a ray of light in the scene that can collide with all the colliding devices. {If (hit. collider! = Null) // collision to the collision generator {if (! Ischoose) {pos1 = new Vector3 (hit. point. x, hit. point. y, hit. point. z); // record the coordinates of the first vertex // Debug. log (pos1); ischoose = true;} else {num ++; pos2 = new Vector3 (hit. point. x, hit. point. y, hit. point. z); // coordinate of the second vertex // Debug. log (pos2); var distance = Vector3.Distance (pos2, pos1); // the distance between two points. Here, var is of the float type and var is used, var automatically detects the type Vector3 ction = (pos2-pos1 ). normalized; // The vector GameObject warnobj = GameObject in the direction of the line to be spent. instantiate (warnline, (pos2-pos1)/2, Quaternion. identity) as GameObject; // clone alert line warnobj. name = "warnline" + num; warnobj. transform. localScale = new Vector3 (100f, 10f, distance * 70); // adjust the size of warnobj according to the parent object. transform. position = warnobj. transform. position + pos1; // The position of warnobj. transform. forward = direction; warnobj. transform. parent = GameObject. find ("pwarnline "). transform; // Debug. log (direction); ischoose = false ;}}}}}}