📜  Unity Bullet 脚本 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:41.510000             🧑  作者: Mango

代码示例1
// 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();
        rb.AddForce(FirePoint.up * bulletForce, ForceMode2D.Impulse);
    }