Answers for "Unity Bullet script"

C#
0

Unity Bullet script

// You need a Firepoint (where the bullet comes from)
    // You need a BulletPrefab to generate a bullet at the Firepoint 
    public Transform FirePoint;
    public GameObject BulletPrefab;
    
    //Decides how fast the bullet shoots
    public float bulletForce = 20f;

    void Update()
    //If the input key is activated then returns the following
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }

    void Shoot()
    {
        GameObject bullet = Instantiate(BulletPrefab, FirePoint.position, FirePoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(FirePoint.up * bulletForce, ForceMode2D.Impulse);
	}
Posted by: Guest on September-30-2021

C# Answers by Framework

Browse Popular Code Answers by Language