把此脚本挂在 Cursor上
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Cursorrr : MonoBehaviour { MeshRenderer cursor;//光标的渲染器 void Start () { cursor = GetComponentInChildren<MeshRenderer>(); cursor.enabled = false; } void Update () { Vector3 HeadPosition = Camera.main.transform.position; Vector3 HeadDir = Camera.main.transform.forward;// 头部的朝向 RaycastHit hitInfo; if( Physics.Raycast(HeadPosition,HeadDir, out hitInfo)) { //表示有选中物体 cursor.enabled = true; transform.position = hitInfo.point;//设置光标位置 //设置光标方向 transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal); } else { cursor.enabled = false; } } }
