unity3D控制手枪的子弹的生成06

每隔一秒点击鼠标左键发射子弹

创建球体(子弹)调整位置和大小

image

创建子弹材质

image

得到子弹生成位置

复制一份子弹,删掉多余属性

image

放到枪下面

image

把Bullet做成预制体

拖放
image

隐藏子弹

image

得到子弹和位置

    public GameObject bulletGO;
    public Transform firePosition;

保存

赋值

image
赋值后可通过代码实时生成子弹

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

public class GunManager : MonoBehaviour
{
    //1.枪旋转角度 xy轴轩轩角度
    private float maxYRotation = 120;
    private float minYRotation = 0;
    private float maxXRotation = 60;
    private float minXRotation = 0;

    //2.枪射击时间 射击间隔时长
    private float shootTime = 1;
    private float shootTimer = 0;

    //7.得到子弹物体和位置信息
    public GameObject bulletGO;
    public Transform firePosition;

    //3.更新
    private void Update() 
    {
        shootTimer += Time.deltaTime;
        if(shootTimer > shootTime)
        {
            //8.射击
            if (Input.GetMouseButtonDown(0))
            {
                //9.实例化子弹
                GameObject bulletCurrent=GameObject.Instantiate(bulletGO,firePosition.position,Quaternion.identity);
            //10.计时器归零 每1秒生成一次
            shootTimer = 0;
			}
        }
        
        //4.旋转 获取鼠标坐标
        float xPosPrecent=Input.mousePosition.x/Screen.width;
        float yPosPrecent = Input.mousePosition.y / Screen.height;

        //5.规定旋转角度最大最小值范围
        float xAngle = -Mathf.Clamp(yPosPrecent * maxXRotation, minXRotation, maxXRotation)+15;
        float yAngle=Mathf.Clamp(xPosPrecent*maxYRotation, minYRotation, maxYRotation)-60;

        //6.赋值给枪的组件值
        transform.eulerAngles=new Vector3(xAngle, yAngle, 0);
    }

}

设置子弹的重力

image

控制子弹的移动

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

public class GunManager : MonoBehaviour
{
    //1.枪旋转角度 xy轴轩轩角度
    private float maxYRotation = 120;
    private float minYRotation = 0;
    private float maxXRotation = 60;
    private float minXRotation = 0;

    //2.枪射击时间 射击间隔时长
    private float shootTime = 1;
    private float shootTimer = 0;

    //7.得到子弹物体和位置信息
    public GameObject bulletGO;
    public Transform firePosition;

    //3.更新
    private void Update()
    {
        shootTimer += Time.deltaTime;
        if (shootTimer > shootTime)
        {
            //8.射击
            if (Input.GetMouseButtonDown(0))
            {
                //9.实例化子弹
                GameObject bulletCurrent = GameObject.Instantiate(bulletGO, firePosition.position, Quaternion.identity);
                //11.获得子弹后 通过刚体组件给子弹添加一个正前方向上的力,以达到让子弹向前运动的效果
                bulletCurrent.GetComponent<Rigidbody>().AddForce(transform.forward * 2000);
                //10.计时器归零 每1秒生成一次
                shootTimer = 0;
            }
        }

        //4.旋转 获取鼠标坐标
        float xPosPrecent = Input.mousePosition.x / Screen.width;
        float yPosPrecent = Input.mousePosition.y / Screen.height;

        //5.规定旋转角度最大最小值范围
        float xAngle = -Mathf.Clamp(yPosPrecent * maxXRotation, minXRotation, maxXRotation) + 15;
        float yAngle = Mathf.Clamp(xPosPrecent * maxYRotation, minYRotation, maxYRotation) - 60;

        //6.赋值给枪的组件值
        transform.eulerAngles = new Vector3(xAngle, yAngle, 0);
    }

}
posted @ 2023-03-04 16:03  flyall  阅读(192)  评论(0)    收藏  举报