- Home /
how to have air control after a rocket jump?
Hey I'm starting Unity and I want to make a game where the player can fire two projectiles that have enough kickback to propel the player in the opposite direction. I also want the player to be be able to walk left and right, but I also want air control. I can't set the velocity of the player's rigidbody to the axis horizontal * speed because that would make my projectiles unable to push the player horizontally. If i were to control the player in the air by adding a force in the horizontal direction, that would make my player accelerate horizontally without any limit to the amount of speed my player can gain from using the arrow keys in the air. I may instead include an explosion force instead of kickback like i have now, but I believe that is irrelevant. How does tf2, and other games, include air control while having physics? I see the soldier can make an explosion and shoots the player in the air, but the player can still have some control in the air. Here is my code:
void Update () {
//direction and speed in x axis
lrd = Input.GetAxisRaw ("Horizontal");
lrs = lrd * lrSpeed;
//check if on ground
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
//jump
if (rb.velocity.y < 0) {
rb.velocity += Vector2.up * Physics2D.gravity.y * fallMultiplier * Time.deltaTime;
} else if (rb.velocity.y > 0 && !Input.GetButton ("Jump")) {
rb.velocity += Vector2.up * Physics2D.gravity.y * lowJumpMultiplier * Time.deltaTime;
}
if (Input.GetButtonDown("Jump") && onGround) {
rb.velocity += Vector2.up * jSpeed;
}
//move player left right
rb.velocity = new Vector2 (lrs, rb.velocity.y);
//shoot projectiles
if (Input.GetMouseButtonDown (0)) {
if (currentArmIndex >= 0) {
shake = CameraShaker.Instance.ShakeOnce(mag,roughness,inTime,outTime);
int index = Random.Range (0, shoot.Length);
shootClip = shoot [index];
audioSource.clip = shootClip;
audioSource.pitch = Random.Range (0.8f, 1.2f);
audioSource.Play ();
arms[currentArmIndex].Launch(fireSpeed);
rb.AddForce (arms [currentArmIndex].normalizedDifference * armKickBack * -1);
currentArmIndex--;
}
}
//reload level
if (Input.GetKeyDown (KeyCode.O)) {
Application.LoadLevel (0);
}
}