【问题标题】:Rotating and balancing HingeJoint2D with mouse drag使用鼠标拖动旋转和平衡 HingeJoint2D
【发布时间】:2016-04-19 16:44:18
【问题描述】:

我正在尝试实现物理,类似于这个游戏:

https://sites.google.com/site/newstudyhall/games/tilt-2

我有一个“手”精灵,它是运动学的,上面有一个 HingeJoint2D。另一个不是 Kinematic 的精灵“Stick”通过 HingeJoint2D 连接到手。我想通过移动手来平衡手上的棍子。

我已手动附上以下脚本。我用鼠标拖动移动手,并在与鼠标移动相反的方向上施加力。但它不像上面提到的游戏那样工作。

Unity 中是否有任何组件可以用来产生此结果或如何实现它?

private Vector3 screenPoint;
private Vector3 offset;


void FixedUpdate()
{
    //ON CLICK
    if (Input.GetButtonDown("Fire1"))
    {
        screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));

    }

    //ON DRAG
    if (Input.GetButton("Fire1"))
    {
        Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);

        //HAND POSITION CHANGE WITH MOUSE DRAG
        Vector2 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
        transform.position = cursorPosition;

        //APPLY FORCE ON TRAY IN OPPOSITE DIRECTION OF MOUSE MOVEMENT
        GameObject.Find("Stick").GetComponent<Rigidbody2D>().AddForce(((cursorPosition.normalized * 5)) * -1, ForceMode2D.Impulse);

    }
}

【问题讨论】:

    标签: unity3d unity3d-2dtools


    【解决方案1】:

    我认为您可以使用rigidbody2d 组件和重力参数以一种非常简单的方式进行这样的平衡。

    Here is a link to a similar question with a very good answer that may help you

    【讨论】:

    • 我看到您已经在使用刚体,我认为您只需要使用已经可用的重力设置,尽管您可能需要对其进行一些自定义以获得游戏所需的平衡效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多