【问题标题】:Unity look at cursor in 2dunity看2d光标
【发布时间】:2017-08-26 15:13:21
【问题描述】:

我正在 Unity 3D 中创建 2D 游戏,但我的玩家轮换出现问题。 它应该旋转到光标,但是当我开始游戏时,它正在看着主摄像头。
我从很多教程中尝试了很多代码,但在大多数情况下,我的播放器只看主摄像头的中心。
这是我现在的代码:

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);


玩家只是看着镜头。我不是专家,但我认为输入存在一些问题,因为我过去尝试过的一些代码确实如此,玩家只是什么都不看
(我的英语不是最好的......请原谅我的错误)

【问题讨论】:

    标签: c# unity3d rotation cursor


    【解决方案1】:

    你可以使用:

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

    【讨论】:

    • 它还在盯着镜头看:/
    • 此代码用于播放器。可以给我截图吗?
    • 播放器附上你的脚本
    • 这是在更新功能中
    • 你能在启动时调试鼠标位置吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多