Skip to content

Instantly share code, notes, and snippets.

@OutlawGameTools
Created March 9, 2015 00:19
Show Gist options
  • Save OutlawGameTools/18d4123f17052131e723 to your computer and use it in GitHub Desktop.
Save OutlawGameTools/18d4123f17052131e723 to your computer and use it in GitHub Desktop.
CS109 - Add force to a RigidBody2D when the mouse button is pressed.
#pragma strict
public var rocket : GameObject; // object to push
public var xSpeed : float = 0; // horizontal speed
public var ySpeed : float = 400; // vertical speed
private var rb2D : Rigidbody2D;
function Start ()
{
rb2D = rocket.GetComponent.<Rigidbody2D>();
}
function OnMouseDown ()
{
// add force to the object
rb2D.AddForce(new Vector2(xSpeed, ySpeed));
}
function OnMouseUp ()
{
// stop adding force
rb2D.AddForce(Vector2.zero);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment