Ginger is not firing bullets when he is in air

ginger is shooting bullets left and right correctly but when i make the player jump, it doesn’t fires any bullets

Share your PlayerMovement.cs script and we can have a look.

See here if you need help formatting the code

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
[SerializeField] float runSpeed = 7f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float climbSpeed = 5f;
[SerializeField] Vector2 deathKick = new Vector2 (30f, 30f);
[SerializeField] GameObject bullet;
[SerializeField] Transform gun;

Vector2 moveInput;
Rigidbody2D myRigidbody;
Animator myAnimator;
CapsuleCollider2D myBodyCollider;
BoxCollider2D myFeetCollider;
float gravityScaleAtFirst;
bool isAlive = true;
SpriteRenderer sprndr;

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    myBodyCollider = GetComponent<CapsuleCollider2D>();
    myFeetCollider = GetComponent<BoxCollider2D>();
    gravityScaleAtFirst = myRigidbody.gravityScale;
    sprndr = GetComponent<SpriteRenderer>();
}


void Update()
{
    if (!isAlive) { return; }
    Run();
    FlipSprite();
    ClimLadder();
    Die();
}

void OnFire(InputValue value)
{
    if (!isAlive) { return; }
    Instantiate(bullet,gun.position,transform.rotation);
}

void OnMove(InputValue value)
{
    if (!isAlive) { return; }
    moveInput = value.Get<Vector2>();
}

void OnJump(InputValue value)
{
    if (!isAlive) { return; }
    if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
    {
        return;
    }
    if (value.isPressed)
    {
        myRigidbody.velocity += new Vector2(0f, jumpSpeed);
    }
}



void Run()
{
    if (!isAlive) { return; }
    Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y);
    myRigidbody.velocity = playerVelocity;

    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;

    myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);

}
void FlipSprite()
{
    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
    if (playerHasHorizontalSpeed)
    {
        transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f);
    }
}

private void ClimLadder()
{
    if (!isAlive) { return; }
    if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Climbing")))
    {
        myRigidbody.gravityScale = gravityScaleAtFirst;
        myAnimator.SetBool("isClimbing", false);
        return;
    }
    Vector2 climbVelocity = new Vector2(myRigidbody.velocity.x, moveInput.y * climbSpeed);
    myRigidbody.velocity = climbVelocity;
    myRigidbody.gravityScale = 0f;

    bool playerHasVerticalSpeed = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
    myAnimator.SetBool("isClimbing", playerHasVerticalSpeed);
}

void Die()
{
    if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemy", "Hazards")))
    {
        isAlive = false;
        myAnimator.SetTrigger("Dying");
        myRigidbody.velocity = deathKick;
        //sprndr.color = Color.black;
    }
}

}

So, not formatting then.

This looks fine. Can you show your input settings? How did you set up the buttons?


when i click on my touchpad the player fire bullets.
using spacebar player jumps
4 arrow keys for moving left right up and down.

I wonder if this isn’t the problem. You are interacting using 2 different controller schemes. Unity needs to change schemes each time. If you jump, it’s using the keyboard. If you then try to shoot with the touchpad, Unity needs to change schemes first. Can you jump with space and then shoot with the left mouse button?

thanks, it’s working

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms