Skip to content

Instantly share code, notes, and snippets.

@OutlawGameTools
Created April 23, 2015 05:11
Show Gist options
  • Save OutlawGameTools/11ab114c8bdcf076d51a to your computer and use it in GitHub Desktop.
Save OutlawGameTools/11ab114c8bdcf076d51a to your computer and use it in GitHub Desktop.
CS109 - Add force to a game object when mouse button is pressed; stop adding when button is up.
#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