我正在尝试实现物理,类似于这个游戏:
https://sites.google.com/site/newstudyhall/games/tilt-2
我有一个“手”精灵,它是运动学的,上面有一个HingeJoint2D。另一个sprite“Stick”不是运动学的,它通过HingeJoint2D连接到hand。我想通过移动手来平衡手上的棍子。
我已附上以下手写脚本。我正在用鼠标拖动来移动手,并在与鼠标移动相反的方向上施加力棒。但它并不像上面提到的游戏那样工作。
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);
}
}发布于 2016-04-20 03:02:17
我认为你可以使用rigidbody2d组件和重力参数以一种非常简单的方式来做这样的平衡。
Here is a link to a similar question with a very good answer that may help you
https://stackoverflow.com/questions/36724525
复制相似问题