Mouse picking code Summary

Source: Internet
Author: User
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 }}

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.