2016-04-19 174 views
0

我想實現物理,類似這樣的遊戲平衡HingeJoint2D:旋轉和拖動鼠標

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); 

    } 
} 

回答