简体   繁体   English

Unity 在 2d 中查看光标

[英]Unity look at cursor in 2d

I'm creating 2D game in Unity 3D, but I have problem with my player rotation.我正在 Unity 3D 中创建 2D 游戏,但我的玩家旋转有问题。 It should rotate to the cursor, but when I start the game, its looking at Main camera.它应该旋转到光标,但是当我开始游戏时,它看着主摄像头。
I've tried lots of codes from lots of tutorials, but in most of it, my player is only looking to the center of Main camera.我已经尝试了很多教程中的很多代码,但在大多数情况下,我的播放器只看主摄像头的中心。
This is my code now:这是我现在的代码:

Vector3 mouse = Input.mousePosition;

    Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(new Vector3 (mouse.x, mouse.y, player.transform.position.y));

    Vector3 forward = mouseWorld - player.transform.position;
    player.transform.rotation = Quaternion.LookRotation (forward, Vector3.up);


Player is only looking to the camera.玩家只是看着相机。 I'm not expert, but I think there is some problem with input, because some codes, that I've tried in past, did, that the player was only looking to nothing我不是专家,但我认为输入存在一些问题,因为我过去尝试过的一些代码确实如此,玩家只是在寻找什么
(I'm not best in english... please apologize my mistakes) (我的英语不是最好的...请为我的错误道歉)

You can use:您可以使用:

Vector3 mouse = Input.mousePosition;

    Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(new Vector3 (mouse.x, mouse.y,transform.position.z));

transform.right = mouseWorld -transform.position;

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

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