Unity的移动、子弹发射,敌人生成脚本

玩家简单控制脚本

using UnityEngine;
using System.Collections;

public class PlaneMove : MonoBehaviour
{

    float speed = 2.0f;   //移动速度
    float rotationSpeed = 90.0f;  //旋转速度
    void Update()
    {
        // 使用上下方向键或者W、S键来控制前进后退
        float translation = Input.GetAxis("Vertical") * speed * Time.deltaTime;
        //使用左右方向键或者A、D键来控制左右旋转
        float rotation = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;

        transform.Translate(0, 0, translation); //沿着Z轴移动
        transform.Rotate(0, rotation, 0); //绕Y轴旋转
    }

}

玩家控制脚本

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    public float speed = 1.5f;//控制移动速度
    public float rotSpeed = 90f;//控制偏移速度


    private GameObject bullet;
    private Transform firePos;

    void Start()
    {
        bullet = Resources.Load<GameObject>("Bullet");
        firePos = this.transform.GetChild(0);

    }

    void Update()
    {
        //前进
        if (Input.GetKey(KeyCode.W))
        {
            float translation = speed;
            transform.Translate(Vector3.forward * Time.deltaTime * speed);
        }
        //后退
        if (Input.GetKey(KeyCode.S))
        {
            float translation = -speed;
            transform.Translate(Vector3.back * Time.deltaTime * speed);
        }
        //左转
        if (Input.GetKey(KeyCode.A))
        {
            float rotation = -rotSpeed;
            transform.Translate(Vector3.left * Time.deltaTime * speed);
        }
        //右转
        if (Input.GetKey(KeyCode.D))
        {
            float rotation = rotSpeed;
            transform.Translate(Vector3.right * Time.deltaTime * speed);
        }
        //抬头
        if (Input.GetKey(KeyCode.J))
        {
            float rotation = -rotSpeed;
            transform.Rotate(rotation * Time.deltaTime, 0, 0);
        }
        //俯身
        if (Input.GetKey(KeyCode.K))
        {
            float rotation = rotSpeed;
            transform.Rotate(rotation * Time.deltaTime, 0, 0);
        }
        //左侧翻身
        if (Input.GetKey(KeyCode.A))
        {
            float rotation = -rotSpeed;
            transform.Rotate(0, rotation * Time.deltaTime, 0);
        }//右侧翻身
        if (Input.GetKey(KeyCode.L))
        {
            float rotation = rotSpeed;
            transform.Rotate(0, rotation * Time.deltaTime, 0);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Attack();
        }

    }

    public void Attack()
    {
        GameObject tempBullet = Instantiate(bullet, firePos.position, firePos.rotation);
        tempBullet.AddComponent<BulletConterl>();
        tempBullet.name = "PlayerBullet";
        Destroy(tempBullet, 5f);
    }
}

子弹脚本

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

[RequireComponent(typeof(BoxCollider))]
[RequireComponent(typeof(Rigidbody))]

public class BulletConterl : MonoBehaviour
{
    void Start()
    {
        GetComponent<BoxCollider>().isTrigger = true;
        GetComponent<Rigidbody>().useGravity = false;
    }
    void Update()
    {
        transform.Translate(Vector3.up * Time.deltaTime * 5f);
    }

    public void OnTriggerEnter(Collider collision)
    {
        switch (collision.tag)
        {
            case "Player":
                if (gameObject.name == "EnemyBullet")
                {
                    GameObject.Find("GameManage").GetComponent<GameManage>().AttackCount(1); 
                    //Destroy(gameObject);
                }
                break;
            case "Enemy":
                if (gameObject.name == "PlayerBullet")
                {
                    GameObject.Find("GameManage").GetComponent<GameManage>().Score(1);
                    Destroy(gameObject);
                    Destroy(collision.gameObject);
                }
                break;
            case "Wall":
                Destroy(gameObject);
                break;
            case "EnemyWall":
                Destroy(gameObject);
                break;
            default:
                /*if (this.name != collision.name)
                {
                    Destroy(gameObject);
                }*/
                break;
        }
    }
}

敌人生成

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

public class GameManage : MonoBehaviour
{
    private GameObject[] enemys;

    private int score;
    private int attackCount;
    private static Text txt;
    void Start()
    {
        Time.timeScale = 0;
        Application.targetFrameRate = 280;
        txt = GameObject.Find("Score").GetComponent<Text>();
        enemys = Resources.LoadAll<GameObject>("Enemys");
        InvokeRepeating("CreateEnemy", 0, Random.Range(0.3f, 3f));
    }

    void Update()
    {
        
    }

    private void CreateEnemy()
    {
        int num = Random.Range(0, enemys.Length);
        GameObject enemy = Instantiate(enemys[num], new Vector3(Random.Range(-2.8f, 2.8f), Random.Range(0.2f, 2.8f), -4.5f), Quaternion.identity);
        enemy.AddComponent<EnemyAI>();
        enemy.GetComponent<EnemyAI>().speed = Random.Range(2, 8);
        enemy.tag = "Enemy";
        Destroy(enemy, 40);
    }

    public void Score(int score)
    {
        this.score += score;
        txt.text = "得分: " + this.score + "\n" + "被打中次数: " + this.attackCount;
    }
    public void AttackCount(int attackCount)
    {
        this.attackCount += attackCount;
        txt.text = "得分: " + this.score + "\n" + "被打中次数: " + this.attackCount;
    }
}

敌人AI发射子弹

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

[RequireComponent(typeof(BoxCollider))]
[RequireComponent(typeof(Rigidbody))]

public class EnemyAI : MonoBehaviour
{
    public int speed;
    private GameObject bulletPrefab;
    private Transform firePos;

    void Start()
    {
        bulletPrefab = Resources.Load<GameObject>("Bullet");
        firePos = transform.GetChild(0);
        InvokeRepeating("Attack", 0, Random.Range(0.8f, 2.2f));
        GetComponent<BoxCollider>().isTrigger = true;
        GetComponent<Rigidbody>().useGravity = false;
    }
    
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed * 0.2f);
    }
    private void Attack()
    {
        GameObject tempBullet = Instantiate(bulletPrefab, firePos.position, firePos.rotation);
        tempBullet.AddComponent<BulletConterl>();
        tempBullet.name = "EnemyBullet";
    }

    public void OnTriggerEnter(Collider collision)
    {
        if (collision.tag == "EnemyWall")
        {
            Destroy(gameObject);
        }
        if (collision.tag == "Player")
        {
            GameObject.Find("GameManage").GetComponent<GameManage>().AttackCount(1);
            Destroy(gameObject);
        }


    }

}

end

评论