unity射击游戏之镜头的控制与射击

注意:射击游戏的镜头需要将鼠标隐藏 (以下代码已实现鼠标隐藏),并在镜头子物体内创建一个物体,该物体挂载了准星图片并将该物体放在镜头中心

镜头控制代码:


using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.Experimental.PlayerLoop;
using static UnityEngine.Experimental.UIElements.EventDispatcher;

public class CamerMove : MonoBehaviour
{
    public float speed;
    public Texture2D mouseSprite;
    public float mouseSensity;
    private float mouseX;
    private float mouseY;
    private Rigidbody2D PlayerRigibody;

    private void Start()
    {
        PlayerRigibody = GetComponent<Rigidbody2D>();
    }



    public void FixedUpdate()
    {
        Move();
        CamerRotation();
        Cursor.lockState = CursorLockMode.Locked;//鼠标隐藏

    }
    private void Move()
    {

            float h = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
            float v = Input.GetAxisRaw("Vertical") * Time.deltaTime * speed;
            this.transform.Translate(h, 0, v);
    }


    private void CamerRotation()
    {
        mouseX = Input.GetAxis("Mouse X") * mouseSensity * Time.deltaTime;
        //Input.GetAxis("Mouse X")返回[-1,1],根据鼠标的移动速度决定数值的大小
        mouseY = Input.GetAxis("Mouse Y") * mouseSensity * Time.deltaTime;
        mouseY = Mathf.Clamp(mouseY, -0.3f, 0.3f);//控制mouseY的最大和最小值


        transform.Rotate(Vector3.up * mouseX);//括号内包括Y轴旋转的角度
        transform.Rotate(Vector3.right* -mouseY*2.5f);//括号内包括Z轴旋转的角度
        this.transform.position=new Vector3(this.transform.position.x,0,this.transform.position.z);//Y轴不变。3D游戏如果Y轴变了会出现遁地情况

        //固定Rotion的Z值
        if (transform.localEulerAngles.z != 0)
        {
            float rotX = transform.localEulerAngles.x;
            float rotY = transform.localEulerAngles.y;
            transform.localEulerAngles = new Vector3(rotX, rotY, 0);
        }
    }
}

射击代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;

public class Shot : MonoBehaviour
{

    RaycastHit hitInfo;
    Ray ray;
    
    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.gameObject.tag == "Enemy")
                {
                    //后坐力
                    this.transform.DOLocalMoveZ(-0.1f, 0.15f).OnComplete(delegate ()
                    { this.transform.DOLocalMoveZ(-0.14f, 0.15f); });

                    Pool.instance.getFormPool(hit.transform);            
                }                
            }
        }      
    }

Pool代码

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

public class Pool : MonoBehaviour
{
    public static Pool instance;
    public GameObject bulletPrefab;
    public int bulletCount;
    private   Queue<GameObject> queuePool = new Queue<GameObject>();

    private void Awake()
    {
        instance = this;
        FillPool();
    }

    public void FillPool()
    {
        for (int i = 0; i < bulletCount; ++i)
        {
            GameObject newBullet = Instantiate(bulletPrefab, this.transform);
            newBullet.transform.SetParent(this.transform);
            returnPool(newBullet);
        }
    }

    public void returnPool(GameObject gameObject)
    {
        gameObject.SetActive(false);
        queuePool.Enqueue(gameObject);
    }

    public void getFormPool(Transform hit)
    {
        if (queuePool.Count <= 0)
        {
            FillPool();
        }
        var outBullet = queuePool.Dequeue();
        outBullet.transform.position = hit.position;
        outBullet.SetActive(true);

        if (this.transform.childCount > 8)
        {
            Debug.Log("sss");
            for (int i = 0; i < 4; ++i)
            {
                Destroy(this.transform.GetChild(i).gameObject);
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值