#Prefab Instantiation issue when flipping 2D IK Sprite

1 messages · Page 1 of 1 (latest)

granite magnet
#

I am having the following issue shown attached. When instantiating my bullet prefab from the designated transform, I cannot shoot in the opposite direction correctly.
My PlayerBullet prefab has its own script to set the velocity of the object once it is spawned. It obviously works in one direction but I feel the way I am flipping the IK sprite might have something to do with it as I do not flip it via the SpriteRenderer but rather through the localScale of the character. Using the SpriteRenderer messes with the 2D IK bones and generally breaks the character.
Here is the PlayerBullet Code:

public class PlayerBullet : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField] float _bulletSpeed;
    private Rigidbody2D _rb;
    
    void Start()
    {
        _rb = GetComponent<Rigidbody2D>();
        _rb.velocity = new Vector2(_bulletSpeed, 0);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Here is the section of the player controller that instantiates the prefab:


            if (_isAttacking && _canFireWeapon)
            {
                _upperBodyAnimator.CrossFade("s_shoot", 0, 0);
               StartCoroutine(FireWeapon(_fireRate));
            }
        IEnumerator FireWeapon(float fireRate)
        {
            _canFireWeapon = false;
            Instantiate(_bulletPrefab, _bulletSpawner.transform.position, transform.rotation);
            yield return new WaitForSeconds(fireRate);
            _canFireWeapon = true;
        }
olive hatch
#

change velocity.x of bullet as the same direction as the player facing?

#

just use your input as your facing direction

granite magnet
#

Would it be feasible to get the direction being faced via the localScale of the sprite?

#

and use that to calculate velocity?

olive hatch
granite magnet
#

    private Vector3 facingLeft = new Vector3(-1, 1, 1);
    private Vector3 facingRight = new Vector3(1, 1, 1);

            switch (Input.GetAxis("Horizontal"))
            {
                case > 0:
                    rb2d.transform.localScale = facingLeft;
                    break;
                case < 0:
                    rb2d.transform.localScale = facingRight;
                    break;
            }
        }
#

Apparently, using the IK system with sprites and flipping the sprites using the .flip() function, breaks everything.

#

So I had to resort to swapping the localScale

olive hatch
#

and should change when you press it only so it record the last input

granite magnet
#

I see what you're getting at, but the sprite flip logic is in PlayerController.cs where as the bullet velocity logic is in PlayerBullet.cs

olive hatch
#

static Input.GetAxis("Horizontal") var you can access from bullet script?

#

i mean you dont have multiple player right?

granite magnet
#

Nope.

#

I thought about that, but its ugly. But 🤷‍♂️

olive hatch
#

yeah

#

local stuff dont change when parent change i think

granite magnet
#

I could also get the player object from the scene using FindObjectOfType<>()

olive hatch
#

so be a hassle if you trying to figure it out just by local transform alone