- Home /
Question by
TeeVee20105 · Nov 16, 2019 at 08:59 PM ·
2d platformerbulletsflipping
How to make it so bullets go where the player faces ( 2d platformer )
Hello i am new to c# and unity. I've got this code.. they do work, bullets fire and work fine. the only problem is they only fire to the left position. is there a way to make it so that i can shoot both directions depending on where the player is facing?
Bullet script
public class BulletScript : MonoBehaviour { Rigidbody2D rb2d; // Start is called before the first frame update void Start() { rb2d = GetComponent(); }
// Update is called once per frame
bool isFacingLeft = false;
void FixedUpdate()
{
rb2d.velocity = new Vector2(300, 0);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("ground"))
{
Destroy(gameObject);
}
}
}
PlayerScript
public class PlayerScript : MonoBehaviour { Animator animator; Object bulletRef;
// Start is called before the first frame update
void Start()
{
bulletRef = Resources.Load("Bullet");
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
animator.Play("Player_Shoot");
GameObject bullet = (GameObject)Instantiate(bulletRef);
bullet.transform.position = new Vector3(transform.position.x + .4f, transform.position.y + .24f, -1);
}
}
}
any help is appreaciated! Thanks
Comment