About the mouse picking code, click an object to display the name of the clicked object ******************************** **************************************** *** it can be achieved by hanging the following javascript code on any object, print the mouse when you click
For the mouse picking code, you can click an object to display the name of the clicked object.
***************************************************************************
You can place the following javascript code on any object. When you click the mouse, the name of the object clicked by the mouse is printed.
function Update () { if (Input.GetMouseButton (0)) { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { Debug.DrawLine (ray.origin, hit.point); print(hit.collider.gameObject.name); } } } ************************************************************************
C # code may be somewhat different
Convert to c # The Code is as follows:
First define public RaycastHit hit;
if (Input.GetMouseButton (0)) { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); ****************************************************************
Assets/scripts/SelectTarget.cs(26,21): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit)' has some invalid arguments
Assets/scripts/SelectTarget.cs(26,21): error CS1620: Argument `#2' is missing `out' modifier
**************************************** * *********************** If (Physics. raycast (ray, out hit) // note that this is different from javascript. If you need to add OUT, an error will be reported, as shown in the {Debug. drawLine (ray. origin, hit. point);} if (hit. collider. gameObject. name = "name of the object to be clicked") {Debug. log ("selected" + hit. collider. gameObject. name );// *************** Click here to display the operation code.
* ************************ For example, to change the color of the selected object to red
GameObject b = Gameobject.Find(hit.collider.gameObject.name);
B. renderer. material. color = Color. red; // For example, change the color to red }}