Skip to content

Instantly share code, notes, and snippets.

@ntakouris
Created January 23, 2015 09:55
Show Gist options
  • Save ntakouris/147fd0f50a7d9ef6316c to your computer and use it in GitHub Desktop.
Save ntakouris/147fd0f50a7d9ef6316c to your computer and use it in GitHub Desktop.
Best game dev code i have written
void Update () {
angle = 0;
bool up = false;
bool down = false;
bool right = false;
bool left = false;
bool vert = false;
bool hor = false;
bool grounded = isOnGround ();
Vector2 velocity = new Vector2(0,0);
if (Input.GetKey (KeyCode.UpArrow)) {
up = true;
//Debug.Log("UpArrow");
} else if (Input.GetKey (KeyCode.DownArrow)) {
down = true;
//Debug.Log("DownArrow");
} else {
vert = true;
//Debug.Log("VertArrows");
}
if (Input.GetKey (KeyCode.RightArrow)) {
right = true;
//Debug.Log("RightArrow");
} else if (Input.GetKey (KeyCode.LeftArrow)) {
left = true;
//Debug.Log("LeftArrow");
} else {
//Debug.Log("HorArrow");
hor = true;
}
if (up && right) {
angle = 315;
float cos = Mathf.Cos(45);
float sin = Mathf.Sin(45);
aimArrowOffsetx = cos + 0.1f;
aimArrowOffsety = sin - 0.07f;
if(grounded){
gameObject.rigidbody2D.AddForce(new Vector2(cos , sin));
}
} else if (up && left) {
angle = 45;
float cos = Mathf.Cos(315);
float sin = Mathf.Sin(315);
aimArrowOffsetx = -cos;
aimArrowOffsety = sin;
if(grounded){
gameObject.rigidbody2D.AddForce(new Vector2(cos , sin));
}
} else if (down && right) {
float cos = Mathf.Cos(225);
float sin = Mathf.Sin(225);
aimArrowOffsetx = cos + 0.15f;
aimArrowOffsety = sin;
angle = 225;
} else if (down && left) {
float cos = Mathf.Cos(135);
float sin = Mathf.Sin(135);
aimArrowOffsetx = cos + 0.5f;
aimArrowOffsety = sin - 1;
angle = 135;
} else if (right) {
angle = 270;
aimArrowOffsetx = 1;
aimArrowOffsety = 0;
if(grounded){
velocity = new Vector2(speed , 0);
}
} else if (left) {
angle = 90;
aimArrowOffsetx = -1;
aimArrowOffsety = 0;
if(grounded){
velocity = new Vector2(-speed , 0);
}
} else if (up) {
angle = 0;
aimArrowOffsetx = 0;
aimArrowOffsety = 1;
if(grounded){
gameObject.rigidbody2D.AddForce(new Vector2(0 , jumpForce));
}
} else if (down) {
aimArrowOffsetx = 0;
aimArrowOffsety = -1;
angle = 180;
}
if (hor && vert) {
isAimKeyPressed = false;
aimArrowOffsetx = 0;
aimArrowOffsety = 0;
//Debug.Log("NoKeys");
} else {
isAimKeyPressed = true;
//Debug.Log("Keys");
}
pAngle = angle;
gameObject.rigidbody2D.velocity = velocity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment