First "bullet" after changing direction goes backwards

I did the changing projectile direction differently, I put the logic in the PlayerMovement script

bool FacingLeft{
        get{
            return transform.localScale.x<0;
        }
    }

    void OnFire(InputValue value)
    {
        
        var fireballProjectile = Instantiate(fireballPrefab,handPosition.position,handPosition.rotation);
        var fireballComponent = fireballPrefab.GetComponent<Fireball>();
        
        if(FacingLeft)
        {
            Debug.Log("Fireball to left");
            var scale = fireballProjectile.transform.localScale;
            fireballProjectile.transform.localScale = new Vector3(scale.x * -1,scale.y,scale.z);
            fireballComponent.fireballSpeed = -fireballSpeed;
        }
        else
        {
            Debug.Log("Fireball to right");
            fireballComponent.fireballSpeed = fireballSpeed;
        }

    }

This almost works, but the first time I fire after switching direction the fireball moves backwards (but the facing of the fireball is correct). Here’s a screenshot. I was facing left, turned to face right, then clicked twice. The fireball on the left of me is the one that spawned first, it’s facing right as it should be, but moving backwards.

I presume that it would work if I did it the same way as in the video, but I’d like to understand what’s going on here.

Hi Stephen,

Good job on developing your own solution. :slight_smile:

Try to log the values of the relevant variables into your console to see if you get the expected values.

I skimmed your code and noticed this line:
var fireballComponent = fireballPrefab.GetComponent<Fireball>();

Do you really want to change the values of the Fireball component attached to the prefab?

2 Likes

Oh thanks, good spot! I think what I intended was fireballProjectile.GetComponent, I’ll try that when I get home today

it worked, thanks!

Fantastic! Have a nice weekend! :slight_smile:

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

Privacy & Terms