[英]Moving object on x axis with mouse move
I am trying to create a game where the player drops a cube from the top of the screen to the bottom, collecting points on the way down depending on where they dropped the cube from (like a Coin Pusher arcade game). 我正在尝试创建一个游戏,玩家将立方体从屏幕的顶部放到底部,并根据他们从何处放下立方体来收集点数(例如投币式街机游戏)。
At the moment I am able to have my cube move to the mouse, but this in both X and Y values. 目前,我可以将多维数据集移动到鼠标,但这在X和Y值中均如此。 See -
见-
Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 mouse = Input.mousePosition;
mouse = mouse;
transform.position = Camera.main.ScreenToViewportPoint(mouse);
I am keen to have movements only on X but am unsure how to achieve this. 我渴望仅在X上移动,但是不确定如何实现这一点。
Thank you. 谢谢。
这可能起作用:
transform.position = new Vector3(Camera.main.ScreenToViewportPoint(mouse).x, 0, 0);
You're doing a few things in your code that are unnecessary, mainly the first and third lines. 您在代码中做了一些不必要的事情,主要是第一行和第三行。 I will try to simplify your code with my answer.
我将尝试用我的答案简化您的代码。
Anyway, you have the mouse's X and Y, so all you need to do is have your object only move to its X position. 无论如何,您都有鼠标的X和Y,因此您所要做的就是仅将对象移动到其X位置。 Right now you assign the object's entire position to match that of the mouse.
现在,您分配对象的整个位置以匹配鼠标的位置。
Vector3 mouse = Camera.main.ScreenToViewportPoint(Input.mousePosition);
transform.position = new Vector3(mouse.x, transform.position.y, transform.position.z);
This will maintain your object's position along other axes, but align it to the horizontal position of your mouse cursor. 这将保持对象在其他轴上的位置,但将其与鼠标光标的水平位置对齐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.