Finally made the projectiles shoot in every direction

ezgif-5-ca7db51b2a

so happy about it, just spent hours trying to find what’s wrong with the code and tried a lot of random solutons until it worked :slight_smile:

here’s the script if there’s anyone interested, and i’ll appreciate any feedback to improve on it.

first you have to make the moveinput variable public in the PlayerMovement script,
then just copy this code to the bullet script(it’s called orb in my case):
public class orb : MonoBehaviour

{

[SerializeField] float orbSpeed = 20f;

Rigidbody2D orbRigidbody;

PlayerMovement player;

CapsuleCollider2D orbCollider;

float xDirection;

Vector2 orbAimInput;

bool playerHasHorizontalSpeed;

bool playerHasVerticalSpeed;

void Start()

{

    orbRigidbody = GetComponent<Rigidbody2D>();

    player = FindObjectOfType<PlayerMovement>();

    xDirection = player.transform.localScale.x;

    orbAimInput = player.moveInput;

    if(xDirection>0)

    {

        transform.Rotate(0f,0f,orbAimInput.y*90f);

        return;

    }

    transform.Rotate(0f,0f,-orbAimInput.y*90f);

    transform.localScale = new Vector2 (-xDirection, 1f);



}

void Update()

{

    playerHasHorizontalSpeed = Mathf.Abs(orbAimInput.x)>Mathf.Epsilon;

    playerHasVerticalSpeed   = Mathf.Abs(orbAimInput.y)>Mathf.Epsilon;

    if(playerHasHorizontalSpeed)

    {

        orbRigidbody.velocity = new Vector2 (orbAimInput.x*orbSpeed, orbAimInput.y*orbSpeed);

        return;

    }

    if(playerHasVerticalSpeed)

    {

        orbRigidbody.velocity = new Vector2 (orbAimInput.x*orbSpeed, orbAimInput.y*orbSpeed);

        return;

    }  

    else if(!playerHasHorizontalSpeed && !playerHasVerticalSpeed)

    {

    orbRigidbody.velocity = new Vector2 (Mathf.Sign(xDirection)*orbSpeed, 0f);

    }

}
}

then the rest are just the ontrigger and oncollision lines the same as what is in the lecture.

5 Likes

ezgif-4-a641bb9acc

i noticed that with a controller the projectiles are at a weird angle, and if you move the stick a little bit they come out slow. so i edited the script and made them only shoot with 45 angles and have a consistent speed no matter how much the stick is moved.
and added a button in the Playermovement script that can lock the player in place so it’s easier to aim (just like in cuphead). again if you have any feedback i’d really appreciate it.
public class orb : MonoBehaviour

{

[SerializeField] float orbSpeed = 20f;

Rigidbody2D orbRigidbody;

PlayerMovement player;

CapsuleCollider2D orbCollider;

float xDirection;

Vector2 orbAimInput;

bool playerHasHorizontalSpeed;

bool playerHasVerticalSpeed;

void Start()

{

    orbRigidbody = GetComponent<Rigidbody2D>();

    player = FindObjectOfType<PlayerMovement>();

    xDirection = player.transform.localScale.x;

    orbAimInput = player.moveInput;

    if(xDirection>0&&Mathf.Abs(orbAimInput.y)>0)

    {

        if(Mathf.Abs(orbAimInput.x)>0)

        {

            transform.Rotate(0f,0f,Mathf.Sign(orbAimInput.y)*45f);

            return;

        }

        transform.Rotate(0f,0f,Mathf.Sign(orbAimInput.y)*90f);

        return;

    }

    if(Mathf.Abs(orbAimInput.y)>0)

    {

        if(Mathf.Abs(orbAimInput.x)>0)

        {

            transform.Rotate(0f,0f,Mathf.Sign(orbAimInput.y)*125f);

            return;

        }

        transform.Rotate(0f,0f,-Mathf.Sign(orbAimInput.y)*90f);

    }

    transform.localScale = new Vector2 (-xDirection, 1f);



}

void Update()

{

    playerHasHorizontalSpeed = Mathf.Abs(orbAimInput.x)>Mathf.Epsilon;

    playerHasVerticalSpeed   = Mathf.Abs(orbAimInput.y)>Mathf.Epsilon;

    if(playerHasHorizontalSpeed&&!playerHasVerticalSpeed)

    {

    orbRigidbody.velocity = new Vector2 (Mathf.Sign(orbAimInput.x)*orbSpeed, 0);

    }

    if(playerHasVerticalSpeed&&!playerHasHorizontalSpeed)

    {

    orbRigidbody.velocity = new Vector2 (0, Mathf.Sign(orbAimInput.y)*orbSpeed);

    }

    if(playerHasHorizontalSpeed&&playerHasVerticalSpeed)

    {

    orbRigidbody.velocity = new Vector2

    (Mathf.Sign(orbAimInput.x)*orbSpeed, Mathf.Sign(orbAimInput.y)*orbSpeed);

    }

    if(!playerHasHorizontalSpeed&&!playerHasVerticalSpeed)

    {

    orbRigidbody.velocity = new Vector2 (Mathf.Sign(xDirection)*orbSpeed, 0f);

    }

}

and the other changes are that i did are: first, i made a new action in the input system and called it “LockAim”, and on playermovement script i added the onlockaim method, changed a little bit the run method, and flipsprite method(so that you can flip your sprite while locked in place)
and the script changes are:
public class PlayerMovement : MonoBehaviour
{
public Vector2 moveInput;
bool LockAimInput;

void OnLockAim(InputValue value)

{

    LockAimInput = value.isPressed;

    myAnimator.SetBool("isWalking", false);

}

void Run()

{

    if(LockAimInput&&feetCollider.IsTouchingLayers(LayerMask.GetMask("Ground", "Ladder", "Water")))

    {myRigidbody.velocity = new Vector2 (0f,0f); 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("isWalking", playerHasHorizontalSpeed);

}

void FlipSprite()

{

    bool playerHasHorizontalInput = Mathf.Abs(moveInput.x) > Mathf.Epsilon;

    if(playerHasHorizontalInput)

    {

    transform.localScale = new Vector2 (Mathf.Sign(moveInput.x), 1f);

    }    

}

Very cool code!
Also like your art.

1 Like

thank you!!! :D. and not really this art is awful… but i improved it a lot now :).

Privacy & Terms