private Camera mainCamera; void Start() { //通过名字找到MainCamera物体,从而获取其身上的Camera组件 //mainCamera = GameObject.Find("MainCamera").GetComponent(Camera)<>; //通过标签tag为MainCamera获取MainCamera物体 The first enabled camera tagged "MainCamera" (Read Only). mainCamera = Camera.main; } void Update() { Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; bool isHit = Physics.Raycast(ray, out hit); //如果射线碰撞到物体 if(isHit) { //do something Debug.Log(hit.collider); } }
// Draws a line in the scene view going through a point 200 pixels // from the lower-left corner of the screen using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour{ Camera cam; void Start() { cam = GetComponent<Camera>(); } void Update() { Ray ray = cam.ScreenPointToRay(new Vector3(200, 200, 0)); Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow); } }
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号