【Unity学习笔记】第一人称射击游戏

1、新建一个地面Plan。
2、搭建好Player模型。把枪的模型拖入,调整好角度。由于是第一人称游戏。把camera也拖入Player下。
在这里插入图片描述
3、编写playerMove脚本,实现asdw控制人物的前后左右移动 空格键跳跃的功能
知识点1:由于是3D第一人称游戏,玩家的移动应以自身坐标轴为准,玩家移动脚本编写为

   		//人物的位移 以自身坐标为准
        transform.Translate(new Vector3(x, 0, y)*speed*Time.deltaTime,Space.Self);

知识点2:玩家地面监测:设置玩家不能在空中跳跃,需要借助射线检测玩家是否碰及物体

 bool IsGround()
    {
    	//打印射线以便观察
    	 Debug.DrawRay(transform.position, Vector3.down,Color.red,1f);
        //以当前物体的中心点为起点向下发射一条长度为distance的射线,射线检测是否触碰到碰撞盒 返回true/false
        //射线检测              以哪一位置为中心  向哪个方向发射 向下的距离 Vector3.down=new Vector(0,-1,0);
        return Physics.Raycast(transform.position,Vector3.down,distance);
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class _PlayerMove : MonoBehaviour
{
    //asdw控制人物的前后左右移动 空格键跳跃
    //改变人物的transform
    public float speed = 5f;
    public float distance = 0.2f;
    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");
        //人物的位移 以自身坐标为准
        transform.Translate(new Vector3(x, 0, y)*speed*Time.deltaTime,Space.Self);
        if (Input.GetButtonDown("Jump"))
        {
          //  Debug.Log(IsGround());
            //获取刚体,给物体添加一个向上的力
            if (IsGround())
            {
                GetComponent<Rigidbody>().velocity = transform.up * speed;
            }
                
        }
    }
    bool IsGround()
    {
    	//打印射线以便观察
    	 Debug.DrawRay(transform.position, Vector3.down,Color.red,1f);
        //以当前物体的中心点为起点向下发射一条长度为distance的射线,射线检测是否触碰到碰撞盒 返回true/false
        //射线检测              以哪一位置为中心  向哪个方向发射 向下的距离 Vector3.down=new Vector(0,-1,0);
        return Physics.Raycast(transform.position,Vector3.down,distance);
    }
}

4、编写PlayerRotation脚本 用鼠标控制玩家旋转

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

public class _PlayerRotation : MonoBehaviour
{
    //鼠标的x轴移动方向及速度
    float x;
    float y;
    public float Speed = 2f;
    public GameObject MainCarm;
    // Start is called before the first frame update
    void Start()
    {
        x = MainCarm.transform.eulerAngles.x;
    }

    // Update is called once per frame
    void Update()
    {
        //若按下鼠标右键
        if(Input.GetMouseButton(1)){
            //人物Y轴变化量
            y = Input.GetAxis("Mouse X")*Speed;
            //物体以Y轴为中心进行旋转                            
            //Quaternion.Euler(0, y, 0) 把欧拉角转化成对应的四元数
            transform.localRotation = transform.localRotation * Quaternion.Euler(0, y, 0);

            //摄像机X轴数值
            x -= Input.GetAxis("Mouse Y") * Speed;
            //限定范围
            x = Mathf.Clamp(x,-60, 45);

            //改变摄像机自身的X轴数值(欧拉角)
            MainCarm.transform.localEulerAngles = new Vector3(x, 0, 0);
            
            //MainCarm.transform.localEulerAngles = new Vector3(x, 0, MainCarm.transform.eulerAngles.z);


        }
    }
}

发射子弹,装填子弹。当装填子弹时,子弹不能发射

 void Update()
    {
        stateInfo = anim.GetCurrentAnimatorStateInfo(0);
        gunAnimStart();
        if (gunType==0)
             text.text = "子弹数量:" +"无穷";
        else
             text.text = "子弹数量:" + buttleNumber;
       //切换枪支
        if (Input.GetKeyDown(KeyCode.Q))
        {

            gunType++;
            gunType=gunType%2;
            playerGunState.Close();
            playerGunState.isbool = true;
            if (gunType == 0)
            {
                object98k.SetActive(true);
                objectM24.SetActive(false);
                
                anim = object98k.GetComponent<Animator>();
                stateInfo = anim.GetCurrentAnimatorStateInfo(0);
                buttleNumber = 10;//初始化
               
            }
            else
            {
                object98k.SetActive(false);
                objectM24.SetActive(true);
                anim = objectM24.GetComponent<Animator>();
                stateInfo = anim.GetCurrentAnimatorStateInfo(0);
            }
        }

        if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.Bolt"))
        {
            anim.SetInteger("shoot", -1);//
        }

        if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.Idle"))  
        {
           // Debug.Log("11111");
            if (Input.GetMouseButtonDown(0))
            {
                
                gunfire();
                if(buttleNumber>0)
                    buttleNumber--;
                anim.SetInteger("shoot", 1);//切换到发射
            }
        }

        playermove();
    }
      public void playermove()
    {
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");
        //人物的位移 以自身坐标为准
        transform.Translate(new Vector3(x, 0, y) * speed * Time.deltaTime, Space.Self);
        if (Input.GetButtonDown("Jump"))
        {
            Debug.Log(IsGround());
            //获取刚体,给物体添加一个向上的力
            if (IsGround())
            {

                GetComponent<Rigidbody>().velocity = transform.up * speed;
            }
        }
    }
  	public void gunfire()
    {
        GameObject bullets = Resources.Load<GameObject>("bullet");
        GameObject bullet1 = Instantiate(bullets, GunPosition.position, GunPosition.rotation);

        Vector3 hitPoint;
        //从屏幕中央位置发射一条射线
        Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
        //射线是否触碰到物体
        if(Physics.Raycast(ray,out raycastHit))
        {
            //Debug.Log()
            hitPoint = raycastHit.point;
            bullet1.transform.LookAt(hitPoint);//将子弹看向发射的点
            //子弹的飞行路径
            Debug.DrawLine(GunPosition.position, hitPoint, Color.red, 3f);
            //camera的  Debug.DrawLine打印的是一段射线, Debug.DrawRay是朝某个位置发射射线
            Debug.DrawLine(Camera.main.transform.position, hitPoint, Color.yellow, 3f);
        }
        

    }

在这里插入图片描述

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值