Uninty 鼠标点击(摄像机发出射线-检测位置)

在这里插入图片描述
平面来触发碰撞,胶囊用红色材质方便观察。
脚本挂载到胶囊上方便操作。
目前实现的功能,鼠标左键点击,胶囊就移动到那个位置上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class c6 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // 鼠标左键
        if (Input.GetMouseButtonDown(0))
        {
            // 摄像头发送射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            // 碰撞信息类
            RaycastHit hit;
            // 碰撞类检测
            bool res = Physics.Raycast(ray, out hit);

            // 如果碰撞到了,hit就有内容了
            if (res == true)
            {
                Debug.Log(hit.point);
                // 将射线碰撞到的坐标给到球的坐标
                transform.position = hit.point;
                // 多检测(返回检测后碰撞的数组)参数:(是否、距离、图层、)
                // RaycastHit[] hits = Physics.RaycastAll(ray, 100, 1 << 10);
                // Debug.Log(hits);
            }
        }
    }
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-11 10:02:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-11 10:02:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-11 10:02:02       87 阅读
  4. Python语言-面向对象

    2024-03-11 10:02:02       96 阅读

热门阅读

  1. Django的上下文

    2024-03-11 10:02:02       45 阅读
  2. Golang如何使用命令行-- flag库

    2024-03-11 10:02:02       47 阅读
  3. habitat中的坑(一):训练模型的时候找不到数据

    2024-03-11 10:02:02       42 阅读
  4. 【RHCSA问答题】第十章 配置和保护SSH

    2024-03-11 10:02:02       39 阅读
  5. Day41| 416 分割等和子集

    2024-03-11 10:02:02       46 阅读
  6. 【FreeRTOS任务调度机制学习】

    2024-03-11 10:02:02       37 阅读