【发布时间】: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);
}
}
【问题讨论】: