【问题标题】:Unity 3D C# camera - nudging/moving camera in direction of mouse or joystickUnity 3D C# 相机 - 朝鼠标或操纵杆方向轻推/移动相机
【发布时间】:2016-11-29 01:16:28
【问题描述】:

我正在制作一个具有 45 度摄像机角度的自上而下的射击游戏。在我的场景中,我将相机设置为玩家跟随玩家。我在像"Secret Ponchos" 这样的几款游戏中看到,镜头会稍微浮动到玩家瞄准的方向。它很微妙,但增加了光泽。

我尝试了几种不同的方法,但不知道如何获取 Lerp 的向量。 有没有办法可以基于mouseMovement进行轻推?如果有怎么办?

【问题讨论】:

  • 欢迎来到stackoverflow。我认为你应该在这里查看提问的最佳方式。因此,您和这里的每个人都会更轻松。您需要帮助我们帮助您,提供更多详细信息。例如:你的相机脚本。
  • 嘿抱歉。这就是 cameraScript 会做的所有事情,所以现在没有相机脚本。我想只需要几行就可以找到正确的向量来 lerp ?
  • 您可以尝试从隐形统一项目中的相机脚本。我以前用过它们,我认为它可以按照你想要的方式工作。看一看。 answers.unity3d.com/questions/691973/…
  • 要做好这件事并不容易 - 一种可能性是使用 Unity 中可用的“弹簧”。这很难。寻找适合您的软件包!

标签: c# unity3d camera unity5


【解决方案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);
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 2016-12-14
    相关资源
    最近更新 更多