简体   繁体   English

Unity-尝试将多维数据集移动到鼠标位置-多维数据集向错误的方向移动

[英]Unity - Trying to move cube to mouse position - cube moves in wrong direction

I'm trying to make a simple game where a cube chases the mouse when the player presses the mouse button down. 我正在尝试制作一个简单的游戏,当玩家按下鼠标按钮时,立方体会追逐鼠标。

Here's my code so far: 到目前为止,这是我的代码:

public class PlayerCubeController : MonoBehaviour {

    public float speed = 1.0f;
    Vector3 targetPos = new Vector3();

    void Start () {
        speed = speed * 0.01f;
    }

    void Update () {
        if (Input.GetMouseButtonDown (0)) {
            Debug.Log (Input.mousePosition);
            targetPos = Input.mousePosition;
            targetPos.z = 0;

        } else if (Input.GetMouseButtonUp (0)) {
            targetPos = transform.position;
        }

        transform.position = Vector3.Lerp (transform.position, targetPos, speed * Time.deltaTime);
    }
}

Unfortunately, the cube never goes in the direction of the mouse; 不幸的是,立方体永远不会朝着鼠标的方向移动。 I can have the mouse in the bottom left of the screen, but the cube will still go to the upper right. 我可以将鼠标放在屏幕的左下角,但多维数据集仍将移到右上角。

Strangely if I put the mouse in the left side of the screen, the cube will go straight up. 奇怪的是,如果将鼠标放在屏幕的左侧,则多维数据集将直线上升。

Can anyone tell me where I went wrong? 谁能告诉我我哪里出问题了?

Your problem is simple : Input.mousePosition defines the mouse position in screen coordinates (from (0,0) to (1920,1080) for example). 您的问题很简单: Input.mousePosition定义鼠标在屏幕坐标中的位置(例如,从(0,0)到(1920,1080))。 If you want to get a 3d point from your mouse position, you have an additional step. 如果要从鼠标位置获取3d点,则需要执行其他步骤。 I see two possibilities : 我看到两种可能性:


Use Camera.main.ScreenToWorldPoint and specify by hand the distance you want from your camera : 使用Camera.main.ScreenToWorldPoint并手动指定您想要的距离相机:

 var v = Input.mousePosition;
 v.z = 10.0;
 v = Camera.main.ScreenToWorldPoint(v);
 // Move your cube to v

Documentation : https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html 文档: https : //docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html


Or use a raycast to get the point on your ground for example using Camera.main.ScreenPointToRay : 或使用光线投射,例如使用Camera.main.ScreenPointToRay来获取地面上的点:

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit, 100))
        // Move your cube to hit.point

Documentation : https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html 文档: https : //docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

In a nutshell you need to convert from screen to world co-ordinates. 简而言之,您需要将屏幕坐标转换为世界坐标。 Here's an example 这是一个例子

https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

Something along the lines of: 类似于以下内容:

 Vector3 mouseP = Input.mousePosition;
 mouseP.z = 10.0;
 Vector3 worldP = Camera.main.ScreenToWorldPoint(mouseP);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM