自学内容网 自学内容网

Unity实现原始的发射子弹效果

  • 1 创建一个炮塔,按下空格-坦克会发射一个小球
  • 2.小球会掉在地上滚下来-添加组件
  • 3 间隔几秒后自动销毁
  • 程序逻辑:1.在场景中创建一个炮塔\子弹拿到代码里的变量里
  • 程序逻辑: 2.if语句检测用户有没有按下空格
  • 程序逻辑: 3.在炮塔坐标的位置,克隆子弹
  • 程序逻辑: 4.接着让子弹用translate()往Y轴方向移动
  • 程序逻辑: 5 接着让子弹添加Rigibody组件

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

public class RoleControl : MonoBehaviour
{
    程序逻辑:1.在场景中创建一个炮塔\子弹拿到代码里的变量里
    //程序逻辑: 2.if语句检测用户有没有按下空格
    //程序逻辑:  3.在炮塔坐标的位置,克隆子弹
    //程序逻辑:    4.接着让子弹用translate()往Y轴方向移动
    //程序逻辑:    5 接着让子弹添加Rigibody组件

    GameObject Tanke001;
    GameObject Zidanmuban001;
    GameObject CloneFather;
    GameObject TempObj;//这个变量用来存储临时克隆的物体子弹

   public float speed = 1;
    void Start()
    {
        Tanke001 = GameObject.Find("MainRole");
        Zidanmuban001 = GameObject.Find("ZidanMuban");
        CloneFather = GameObject.FindGameObjectWithTag("FatherClone");
        
    }

    // 
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)||Input.GetMouseButtonDown(0))
        {
            //用户按下了鼠标左键或者空格 克隆
            CloneZIdan();
        }

        if (TempObj!=null)
        {
            TempObj.transform.Translate(new Vector3(0, 1* speed*Time.deltaTime, 0));
        }

        GameObject.Destroy(TempObj, 3f);
    }

    void CloneZIdan()//用来发射
    {
        Debug.Log("这里去发射子弹");

        TempObj =   GameObject.Instantiate(Zidanmuban001, new Vector3(Tanke001.transform.position.x, Tanke001.transform.position.y, Tanke001.transform.position.z+1),Quaternion.Euler(90,0,0), CloneFather.transform);
        TempObj.AddComponent<Rigidbody>();
     }
}
 


原文地址:https://blog.csdn.net/leoysq/article/details/142375965

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!