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

}

}

相关推荐
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦6 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl7 小时前
Mysql测试题
android·adb
游戏开发爱好者810 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号10 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h12 小时前
Android原生Dialog
android
梁同学与Android13 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
Misha韩13 小时前
React Native 基础tabBar和自定义tabBar - bottom-tabs
android·react native
iHero13 小时前
【Nextcloud】在 Ubuntu 22.04.3 LTS 上的 Nextcloud Hub 10 (31.0.2) 后台任务cron 的优化
android·linux·ubuntu·nextcloud
yuanlaile17 小时前
Flutter Android打包学习指南
android·flutter·flutter打包·flutter android