【问题标题】:Unity Mouse Input without Ray/Raycast and Colliders没有 Ray/Raycast 和 Colliders 的 Unity 鼠标输入
【发布时间】:2018-10-28 15:48:25
【问题描述】:

首先感谢您的宝贵时间。我在这方面很新,所以我有点挣扎。我正在尝试制作一个不依赖于碰撞器或光线投射但仅依赖于鼠标增量和相机位置的拖放和释放射击游戏。我尝试完成的方法是将鼠标位置 (x,y) 映射到速度方向,例如 new Vector3(mouseX, 0f, mouseY) 然后围绕 Y 轴旋转它以匹配屏幕上的视觉拖动.

    void OnMouseUp()
{
    releasePos = Input.mousePosition;
    Vector2 m_direction = releasePos - initialPos;
    Vector3 direction = new Vector3(m_direction.x, 0, m_direction.y);
    float userDrag = Mathf.Clamp(direction.magnitude, 0f, maxVelocity);
    direction = Translation(direction);
    direction = Vector3.Normalize(direction);
    rigidbody.velocity = (direction * userDrag) * force;
}

private Vector3 Translation(Vector3 direction)
{
    Vector3 camPos = Camera.main.transform.position;
    camPos.y = 0;
    float angle = Vector3.Angle(new Vector3(0f, 0f, 1f), camPos);
    Quaternion translatedAngle = Quaternion.AngleAxis(angle, Vector3.up);
    direction = translatedAngle * direction;

但是随着角度的变化,它有点无法满足我的要求。有没有办法可以避免一堆 if,else 角度值的语句或更短的方法?

Example

【问题讨论】:

  • 你是想将子弹射到鼠标位置还是从
  • adn unity 翻转y,所以你需要Screen.height-mouseposition.y 而不是mouseposition.y
  • @PrinceOfRavens 因为我得到了增量,所以我认为它的效果很好。我正在尝试拖动/释放,就好像它是“弹弓”一样。
  • 啊,好吧,这听起来很对...所以您从鼠标发射...使用重力?将目标设置为弹丸的原始位置,然后在更新中设置对象以查看目标,然后您可以添加前进速度。有了重力,你会得到一个漂亮的弧线,没有它会永远飞翔。我想我有这个,但不幸的是,它有对撞机。你如何在没有碰撞器的情况下检测到命中?只是好奇。
  • @PrinceOfRavens 很好......我正在使用对撞机来激活该方法,但我没有使用它们来检测方向。这是一款高尔夫射击游戏,因此它本质上是 3d 世界中的 2d 动作,因为我只使用 x 和 z 值来表示速度。我正在苦苦挣扎的是,我无法始终让我的方向相对于相机位置旋转。我要添加一张图片,这样你就可以看到我在说什么。

标签: unity3d input controls mouse


【解决方案1】:

OnMouseUp 本质上是一种碰撞方法。

意思是,鼠标必须位于该代码所附加到的游戏对象的游戏对象碰撞器上。如果您想摆脱使用对撞机,那么您需要摆脱这种方法。

通用的工作方式——在任何地方说“鼠标按钮是向上还是向下?”是Input 类中可用的静态方法:

  • Input.GetMouseButtonDown()按下鼠标后单帧为真
  • Input.GetMouseButtonUp()松开鼠标后单帧为真
  • Input.GetMouseButton()按下鼠标按钮的任何帧都为真(否则为假)

【讨论】:

  • 我使用 OnMouseDown/Up 只是为了确保初始点击是在我试图移动的游戏对象上。如果玩家点击屏幕上的随机点,我不想激活射击。
  • 您也可以通过其他方式来实现。使用光线投射(这就是 OnMouseDown 的工作方式)。
【解决方案2】:

所以我想我终于有了一个解决方案,我想我会把它分享给可能感兴趣的人。

挑战是根据相机位置旋转鼠标三角洲的方向,以便在拖动和释放球进行推杆时为玩家提供自然的氛围。我做了一些测试,看看我的代码在哪里正确旋转增量方向(因此它与屏幕匹配)并意识到当我“从”(0,0,1)进行比较时,当相机的 x 位置为正时它可以工作美好的。但是当 x 为负时,它不会因为Vector3.Angle 不会返回负值就我而言所以我只是将结果乘以负数。似乎有效。

这是最终代码:

    private void Start()
{
    rigidbody = GetComponent<Rigidbody>();
}

void OnMouseDown()
{
    ballPos = Input.mousePosition;
}

void OnMouseUp()
{
    Vector2 m_direction = Input.mousePosition - ballPos;
    Vector3 direction = new Vector3(m_direction.x, 0, m_direction.y);
    float userDrag = Mathf.Clamp(direction.magnitude, 0f, maxVelocity);
    direction = Translation(direction);
    direction = Vector3.Normalize(direction);
    rigidbody.velocity = (direction * userDrag) * force;
}

private Vector3 Translation(Vector3 direction)
{
    float angle = GetAngle();
    Quaternion translatedAngle = Quaternion.AngleAxis(angle, Vector3.up);
    direction = translatedAngle * direction;
    return direction;
}

private float GetAngle()
{
    Vector3 camPos = Camera.main.transform.position;
    camPos.y = 0;
    if (camPos.x > 0)
    {
        angle = Vector3.Angle(Vector3.forward, camPos);
    }
    else if (camPos.x <0)
    { angle = -Vector3.Angle(Vector3.forward, camPos); }
    return angle;
}

如果您对代码有任何建议,请分享。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2022-08-07
    相关资源
    最近更新 更多