2016-04-19 15 views
0

でHingeJoint2Dのバランスをとる私はこのゲームに似て物理学を実装しようとしています:が回転し、マウスドラッグ

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

私は動である「ハンド」スプライトを持っており、それにHingeJoint2Dを持っています。キネマティックではない別のスプライト「スティック」は、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); 

    } 
} 

答えて

1

私はあなたが非常に簡単な方法でそのようbalacing行うにrigidbody2dコンポーネントと重力パラメータを使用できると思います。

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

+0

私はあなたがすでにrigidbodiesを使用していることがわかり、私はあなたがゲームのために必要なbalacing効果を得るためにそれを少しカスタマイズする必要があるかもしれませんが、あなただけの、すでにavaliable重力設定を使用する必要があると思います。 –

関連する問題