首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用鼠标拖动来旋转和平衡HingeJoint2D

使用鼠标拖动来旋转和平衡HingeJoint2D
EN

Stack Overflow用户
提问于 2016-04-20 00:44:18
回答 1查看 296关注 0票数 0

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

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

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

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

Unity中有没有组件,我可以用来产生这个结果,或者我如何实现它?

代码语言:javascript
复制
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);

    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-04-20 03:02:17

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36724525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档