unity获取点击事件向量 [英] Unity obtaining Vector of click event

查看:95
本文介绍了unity获取点击事件向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Unity的Vector3方法 ScreenToWorldPoint .

I'm utilizing Unity's Vector3 method, ScreenToWorldPoint.

简而言之,我可以在GameObject上的任意位置单击,并获得游戏中单击位置的Vector3.但是,我获得的结果是Vector3直接位于相机的前面,而不是我真正单击场景中给定GameObject的表面的地方.

In short, I can click anywhere on a GameObject and obtain the Vector3 of where the click was in the game. However the result I'm obtaining is the Vector3 directly in front of the camera, rather then where I'm truly clicking on the surface of a given GameObject in the scene.

我想要在GameObject表面上单击的确切位置的坐标.

I want the coordinates of exactly where I click on the surface of a GameObject.

推荐答案

您要将射线从相机射线投射到对象.有关更多信息,请参见帮助页面.手册:相机发出的光线

You want to Raycast from the camera to the object. See the help pages for more Manual: Rays from the camera

using UnityEngine;
using System.Collections;

public class ExampleScript : MonoBehaviour {
    public Camera camera;

    void Start(){
        RaycastHit hit;
        Ray ray = camera.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit)) {
            Transform objectHit = hit.transform;

            // Do something with the object that was hit by the raycast.
        }
    }
}

这篇关于unity获取点击事件向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆