unity3D设置手枪的动画与子弹的自动销毁07

取消手枪自动播放动画

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);

                //12.获得组件后做操作
                gameObject.GetComponent<Animation>().Play();
                //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 BulletManager : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine("DestroySelf");
    }
    IEnumerator DestroySelf()
    {
        yield return new WaitForSeconds(2);
        Destroy(this.gameObject);
    }
        
}

显示bullet apply

image
image
然后再隐藏

添加tag 解决克隆

image
image
选中tag apply
image

再隐藏Bullet

posted @ 2023-03-04 17:43  flyall  阅读(238)  评论(0)    收藏  举报