Question by
mike1233 · Feb 06, 2017 at 10:00 PM ·
scripting problembutton2d game
MAKE player move in falling position when no button is being pressed.
MAKE player move in falling position when no button is being pressed. so when he is flying and button pressed moves upward image but if no button is pressed he is falling downward with different image
I have the image ready but I don't know how to code this
2d game
public class hok : MonoBehaviour
{
public float upForce;
public float downForce;
public float forwardSpeed;
public bool isDead = false;
Animator anim;
bool flap = false;
void Start()
{
anim = GetComponent<Animator> ();
GetComponent<Rigidbody2D>().velocity = new Vector2 (forwardSpeed, 0);
}
void Update()
{
if (isDead)
return;
if (Input.GetKeyDown("g"))
flap = true;
}
void FixedUpdate()
{
if (flap)
{
flap = false;
anim.SetTrigger ("Flap");
GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, 0);
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, upForce));
}
if (Input.GetKeyDown("t"))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (-0, downForce));
}
}
int hit = 0;
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Wall")
{
hit++;
if(hit >= 25)
Destroy(gameObject);
}
if(other.gameObject.tag == "Wall")
{
GetComponent<AudioSource>().Play();
}
}
}
Comment