统一获取鼠标位置 [英] Getting mouse position in unity

查看:80
本文介绍了统一获取鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将对象移动到鼠标位置.但这给了我300的大x值,但在那个位置,预置对象的x位置是-4.

I'm trying to move a object to the mouse position. But it's giving me large x value like 300 but at that place the pre placed object's x position is -4.

rigidBody.velocity = new Vector3(Input.mousePosition.x, EndPointY, 0)*4;

那么我如何获得当前的鼠标位置?

So how can I get the current mouse position?

谢谢..

推荐答案

Input.mousePosition 将为您提供鼠标在屏幕上的位置(像素).您需要使用 Camera.ScreenToWorldPoint()将这些像素转换为世界单位.

Input.mousePosition will give you the position of the mouse on screen (pixels). You need to convert those pixels to the world units using Camera.ScreenToWorldPoint().

您可以点击此链接了解如何使用鼠标拖动3d对象,或者您可以复制此代码以将对象从当前位置移动到鼠标位置.

You can follow this link to learn how to drag a 3d object with the mouse or you can copy this code to move an object from the current position to the mouse position.

 //the object to move
public Transform objectToMove;

 void Update()
 {
     Vector3 mouse = Input.mousePosition;
     Ray castPoint = Camera.main.ScreenPointToRay(mouse);
     RaycastHit hit;
     if (Physics.Raycast(castPoint, out hit, Mathf.Infinity))
     {
         objectToMove.transform.position = hit.point;
     }
 }

这篇关于统一获取鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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