1
\$\begingroup\$

So I tried to make a shooting mechanism for my game. My player can shoot in the direction pointed by the mouse. My player can also turn around, facing the other way as he walks. The firing mechanism is fine when my player is facing right, but when my player is facing left (I used transform.localscale and set my x scale to a negative value so my player sprite is flipped) the gun is also reversed, i solved the problem on the gun, but when i shoot the bullet, the first bullet fired after i turn around will go in the wrong direction. I set my weapon as child of an empty game object named gunHolder like this:

enter image description here

When it play, it looks like this, when i trun around and shots my first bullet, the bullet go in mirror direction:

enter image description here

My shooting code look like this:

void Update()
    {
        
        Vector3 difference=Camera.main.ScreenToWorldPoint(Input.mousePosition)-transform.position;
        float rotZ=Mathf.Atan2(difference.y,difference.x)*Mathf.Rad2Deg;
        
        if(player.transform.localScale.x==1)turnOffset=0;
        else turnOffset=180;

        transform.rotation=Quaternion.Euler(0f,0f,rotZ+offset+turnOffset);
        if(timeBtwShots<=0){
            if(Input.GetMouseButtonDown(0)){

            Instantiate(projectile,shotPoint.position,transform.rotation);

            float transX= (float)(player.transform.localScale.x*0.3);
            projectile.transform.localScale= new Vector3(transX,projectile.transform.localScale.y,projectile.transform.localScale.z);

            BulletBehaviour bulletBehaviour=projectile.GetComponent<BulletBehaviour>();
            if(player.transform.localScale.x==1){
                bulletBehaviour.flip=false;
            }
            else{
                bulletBehaviour.flip=true;
                Debug.Log("flip");
            }
            timeBtwShots=startTimeBtwShots;
        }
        }else{
            timeBtwShots-=Time.deltaTime;
        }
        
    }

So I flip my weapon based on where my player is facing. I handle my player animation when facing another direction by changing transform.localscale.x:

scale=transform.localScale;
if (Input.GetKey(KeyCode.A))
     {
          isMoving=true;
          Vector2 movement = transform.right.normalized * -moveSpeed;
          rb.AddForce(movement);
          scale.x=-1;
     }
     else if (Input.GetKey(KeyCode.D))
     {
          isMoving=true;
          Vector2 movement = transform.right.normalized * moveSpeed;
          rb.AddForce(movement);
          scale.x=1;
     }

And my bullet script look like this:

void Update()
    {
        if(!flip){
            transform.Translate(Vector2.right*speed*Time.deltaTime);
            
            }
        else {
            transform.Translate(Vector2.left*speed*Time.deltaTime);
            }
    }

Based on where my player facing, i change my bullet direction(I did this because before, my bullet went in the wrong direction if my player was facing left). Now my bullets are facing the right direction, it's just that the first bullet that is fired after my player turns around always goes in the wrong direction (the opposite direction to the direction it should be, even the sprites are reversed). Anyone know why this happening and how to solve this?

\$\endgroup\$
0
\$\begingroup\$

So the problem is in instantiate. I instantiate the bullet before i set the condition. I try to move the instantiate function after the else statement and it works

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.