Have arrow flip directions

instead of a bullet, I’m using an arrow. However, I can’t get it to flip so that when I’m firing to the left, the arrow sprite flips to also shoot to the left.

2 Likes

Check which direction you are shooting, and then you can use flipX on the SpriteRenderer to flip it. Only the image will be flipped, so if you want to also flip other components along with it, ex. colliders, you can set transform.localScale to have a negative x (or positive if it’s already negative)

I just tossed transform.localScale= new Vector2(Mathf.Sign(myRigidBody.velocity.x),1f); into my update and it worked for my throwing knife. I think it means there is technically a single frame where it’s backwards but… That’s life.

3 Likes

you could add that to the code that instantiates the knife, then you won’t have the ‘backwards’ frame. If you have it in Update(), it will go ‘backwards’ if you turn around while the knife is in flight

The code is this;

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

public class Shuriken : MonoBehaviour
{
    Rigidbody2D myRigidBody; 
    [SerializeField] float bulletSpeed = 20f; 
    PlayerMovementScript player;
    float xSpeed;
    void Start()
    {
        myRigidBody = GetComponent<Rigidbody2D>();
        player = FindObjectOfType<PlayerMovementScript>();
        xSpeed = player.transform.localScale.x * bulletSpeed;
    }

    void Update()
    {
        myRigidBody.velocity = new Vector2(xSpeed, 0f);
        transform.localScale= new Vector2(Mathf.Sign(myRigidBody.velocity.x),1f);
    }

   // void OnTriggerEnter2D(Collider2D other)
   // {
   //     if(other.tag == "Enemy")
   //     {
   //         Destroy(other.gameObject);
   //     }
   //     Destroy(gameObject);
   // }

    void OnCollisionEnter2D(Collision2D other)
    {

        if(other.collider.tag == "Enemy")
        {
             Destroy(other.gameObject);
        }
        Destroy(gameObject);
    }
}

And it does not turn around when I do

image
As you can see orientation persists…

1 Like

Yeah, sorry. I saw myRigidbody and assumed it was on the player script. Didn’t think that the knife had its own rigidbody

Privacy & Terms