[英]How do I make the player move towards the cursor Unity3D
I have this 3D game I'm developing我有这个 3D 游戏我正在开发
In this game I am a roll of toilet paper在这个游戏中我是一卷卫生纸
If I press space, I get yeeted towards the cursor如果我按空格键,我会被吸引到 cursor
I started using raycasts to do this, but that didn't work out, AT ALL我开始使用光线投射来做到这一点,但这并没有成功,根本
So, how do I get the toilet paper to jump towards the cursor?那么,如何让卫生纸跳向 cursor? (how do I get the direction for where the cursor is)
(我如何获得 cursor 所在位置的方向)
So from what I understood, you want your game object to move in 3D space to the direction of your cursor, which is in 2D space.因此,据我了解,您希望您的游戏 object 在 3D 空间中移动到您的 cursor 的方向,这是在 2D 空间中。 You could try to get the game object's world location and use Camera.WolrdToScreenPoint() .
您可以尝试获取游戏对象的世界位置并使用Camera.WolrdToScreenPoint() 。 This gives you Vector3 where x and y are screen coordinates and z is camera world z coordinate.
这为您提供了 Vector3,其中 x 和 y 是屏幕坐标,z 是相机世界 z 坐标。 Then you can get your cursor position with Input.mousePosition .
然后你可以用 Input.mousePosition 得到你的 cursor position 。 Now you have two points that you can use to calculate direction.
现在你有两个点可以用来计算方向。 (You decide if you want to use camera z position or set it to zero.)
(您决定是否要使用相机 z position 或将其设置为零。)
Vector3 payerScreenPos = cam.WorldToScreenPoint(player.transform.position);
Vector3 direction = (playerScreenPos - Input.mousePosition).normalized;
You need to have a reference to the current camera (cam) and to your player.您需要参考当前相机(cam)和播放器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.