首页 文章

Unity 3D C#相机 - 在鼠标或操纵杆的方向上轻推/移动相机

提问于
浏览
0

我正在制作一个45度摄像角度的自上而下射击游戏 . 在我的场景中,我将相机冷却到玩家跟随玩家 . 我已经在一些像"Secret Ponchos"这样的游戏中看到过,相机略微浮动到玩家瞄准的方向 . 它很微妙,但增加了润色 .

我尝试了几种不同的方法,但不知道如何获得Lerp的Vector . 有没有办法可以基于mouseMovement进行微移?如果是这样如何?

1 回答

  • 1

    要获得用于相机控制Lerp的位置,您只需要确定您希望相机轻推的方向并将其添加到玩家的位置 .

    一种选择是使用transform.forward来使用玩家面向的方向,但这需要您旋转玩家角色 .

    //Assign the player's transform here
    public Transform Target;
    
    Vector3 GetNudgeDirection () {
         return Target.forward;
    }
    

    另一种方法是获得鼠标相对于玩家的方向 .

    public Transform Target;
    
    Vector3 GetNudgeDirection () {
       //Get the position of the mouse
       Vector3 mousePos = Input.mousePosition;
       mousePos.z = -Camera.main.transform.position.z;
       Vector2 inputPos = Camera.main.ScreenToWorldPoint(mousePos);
       //returns direction from the player to the mouse pos.
       return (inputPos - (Vector2)Target.position).normalized;
    }
    

    然后,您可以将微移方向添加到目标的位置,以获得相机应该瞄准的位置 .

    //This field determines how far to nudge
    private float nudgeDistance = 2f;
    
    Vector3 GetTargetPosition () {
         return Target.position + (GetNudgeDirection() * nudgeDistance);
    }
    

    请记住,目标位置是您的相机应该看的位置,而不是它应该移动到的位置!因此,当您实际移动相机时,请向目标位置添加偏移量,以便保持其距离 .

相关问题