Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Easy, Add downward force

$
0
0
I have tried coding to add a downward force , please note I have tried many things not just what you currently see, I want on the "t " button press for gameobject to move downward just as the upforce goes up, I want same force to go down when pressed thanks using UnityEngine; using System.Collections; public class dog : MonoBehaviour { public float upForce; //upward force public float downForce; // downw force public float forwardSpeed; //forward movement speed public bool isDead = false; Animator anim; bool flap = false; void Start() { anim = GetComponent (); GetComponent().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 ().velocity = new Vector2 (GetComponent ().velocity.x, 0); GetComponent ().AddForce (new Vector2 (0, upForce)); if (Input.GetKeyDown("t")) //...zero out the dog current y velocity before... GetComponent().velocity = new Vector2(GetComponent().velocity.x, 0); GetComponent ().AddForce (new Vector2 (-0, downForce)); } } void OnCollisionEnter2D(Collision2D other){ if(other.gameObject.tag == "Wall") { isDead = true; GetComponent().Play(); anim.SetTrigger ("Die"); } } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>