当前位置:网站首页>Unity Fighter
Unity Fighter
2022-07-31 04:29:00 【MaNongSa】
提示:以下是本篇文章正文内容
简介
Simple target game
效果

素材
下载链接:点这里
代码实现


0、简单行走
using UnityEngine;
public class Head : MonoBehaviour
{
private Transform head;
private Transform body;
public float MoveSpeed;
public float RositionSpeed;
void Start()
{
head = transform;
body = transform.parent;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float hroitzonal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(hroitzonal, 0, vertical);
if(dir != Vector3.zero)
{
body.Translate(dir * Time.deltaTime * MoveSpeed);
}
float mousex = Input.GetAxis("Mouse X");
if(mousex != 0)
{
body.Rotate(Vector3.up, mousex * 120 * Time.deltaTime* RositionSpeed);
}
float mousey = Input.GetAxis("Mouse Y");
if (mousey != 0)
{
head.Rotate(Vector3.left, mousey * 120 * Time.deltaTime* RositionSpeed);
}
}
}
1、子弹Bullet脚本
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float bulletSpeed;
public float RositionLerpSpeed;
private void Start()
{
Invoke("DestoryTshi",3f);
}
private void OnTriggerEnter(Collider other)
{
if (other)
{
Destroy(gameObject);
}
if (other.gameObject.tag == "Bazi")
{
//声音??????
AudioManager.Instance.playAudio(GamaMagager.Instance.audioConfig.hit the sound);
Debug.Log("打到了!");
other.transform.Rotate(new Vector3(-90, 0, 0));
Destroy(other.transform.gameObject);
Ivensory.Instance.CreateNewObj();
}
}
}
2、实现开枪Gun脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : Single<Gun>
{
private Transform muzzleTran;
private Transform muzzleSpecialEffectsTran;
private LineRenderer lineRenderer;
public GameObject bulletPrefab;
private bool isShowRay;
public float jumpSpeed;
public GameObject muzzleSpecialEffects;
public bool isLuo;
private bool isShoot;
private float timerValue=1;
private float timer;
private void Start()
{
muzzleTran = GameObject.FindWithTag("Gun").transform;
muzzleSpecialEffectsTran = GameObject.FindWithTag("muzzleSpecialEffectsTran").transform;
lineRenderer = GameObject.FindWithTag("Line").GetComponent <LineRenderer>();
lineRenderer.gameObject.SetActive(false);
timer = timerValue;
}
private void Update()
{
if (timer > 0)
{
timer -= Time.deltaTime;
}
if (timer < 0)
{
timer = 0;
isShoot = true;
}
if (Input.GetKeyDown(KeyCode.Mouse1))
{
isShowRay = !isShowRay;
}
if (isShowRay)
{
Debug.DrawRay(muzzleTran.transform.position, muzzleTran.forward * 100, Color.red);
lineRenderer.gameObject.SetActive(true);
}
else
lineRenderer.gameObject.SetActive(false);
if (Input.GetKeyDown(KeyCode.Mouse0)&& isShoot)
{
//声音??????
AudioManager.Instance.playAudio(GamaMagager.Instance.audioConfig.shot);
Instantiate(muzzleSpecialEffects, muzzleSpecialEffectsTran.position, muzzleTran.rotation);
Bullet bullet =Instantiate(bulletPrefab,muzzleTran.transform.position,muzzleTran.rotation).GetComponent<Bullet>();
bullet.GetComponent<Rigidbody>().velocity = muzzleTran.forward * bullet.bulletSpeed;
timer = timerValue;
isShoot = false;
}
if (Input.GetKeyDown(KeyCode.Space) && isLuo)
{
//声音??????
AudioManager.Instance.playAudio(GamaMagager.Instance.audioConfig.跳);
transform.parent.parent.GetComponent<Rigidbody>().velocity = transform.parent.parent.up * jumpSpeed;
isLuo = false;
}
}
}
3、The target is limited to10个,It's common to knock one out,靶子Ivensory管理脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ivensory : Single<Ivensory>
{
private Transform Left;
private Transform Right;
private Transform randomTran;
public GameObject BaZiObj;
private int index;
private float randomTranValue;
[SerializeField]
public List<GameObject> BaZiObjList = new List<GameObject>();
private void Start()
{
Left = GameObject.FindWithTag("Left").transform;
Right = GameObject.FindWithTag("Right").transform;
ForeachBaZi();
}
private void ForeachBaZi()
{
StartCoroutine(Move());
}
IEnumerator Move()
{
for (int i = 0; i < 10; i++)
{
randomTran = Random.Range(0, 10) >= 5 ? randomTran = Left : randomTran=Right;
if(randomTran == Left)
{
randomTranValue = -0.2f;
}
else if (randomTran == Right)
{
randomTranValue = 0.2f;
}
yield return new WaitForSeconds(2f);
GameObject gameObject = Instantiate(BaZiObj,new Vector3(randomTran.position.x- randomTranValue, randomTran.position.y,randomTran.position.z),Quaternion.identity);
BaZiObjList.Add(gameObject);
gameObject.transform.SetParent(transform);
}
}
public void CreateNewObj()
{
StartCoroutine(StectionMove());
}
IEnumerator StectionMove()
{
for (int i = 0; i < BaZiObjList.Count; i++)
{
if (BaZiObjList[i] == null)
{
BaZiObjList.RemoveAt(i);
}
}
yield return new WaitForSeconds(1f);
GameObject gameObject = Instantiate(BaZiObj, new Vector3(randomTran.position.x - randomTranValue, randomTran.position.y, randomTran.position.z), Quaternion.identity);
BaZiObjList.Add(gameObject);
}
}
4、Determine if you can jump againLuo 脚本
using UnityEngine;
public class Luo : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "land"&& Gun.Instance)
{
Gun.Instance.isLuo = true;
}
}
}
5、The target movesTargetMove脚本
using UnityEngine;
public class TargetMove : MonoBehaviour
{
private Transform Left;
private Transform Right;
private bool isGoOnMove;
public float MoveSpeed;
private void Start()
{
Left = GameObject.FindWithTag("Left").transform;
Right = GameObject.FindWithTag("Right").transform;
isGoOnMove = true;
}
private void Update()
{
Move();
}
private void Move()
{
if (transform.position.x > Left.position.x && isGoOnMove)
{
transform.position += -transform.right * Time.deltaTime * MoveSpeed;
if (Vector3.Distance(transform.position,Left.position) < 1f)
{
isGoOnMove = false;
}
}
else if(transform.position.x < Right.position.x && !isGoOnMove)
{
transform.position += transform.right * Time.deltaTime * MoveSpeed;
if (Vector3.Distance(transform.position, Right.position) < 1f)
{
isGoOnMove = true;
}
}
}
}
6、切换武器WeaponCut 脚本
using UnityEngine;
public class WeaponCut : MonoBehaviour
{
public Transform storeTran;
private bool isCut;
private float timerValue=1;
private float timer;
private void Start()
{
isCut = true;
timer = timerValue;
}
private void Update()
{
if (timer > 0)
{
timer -= Time.deltaTime;
}
if (timer < 0)
{
timer = 0;
isCut = true;
}
if (Input.GetKeyDown(KeyCode.Alpha1)&& isCut)
{
storeTran.GetChild(2).gameObject.SetActive(true);
storeTran.GetChild(1).gameObject.SetActive(false);
timer = timerValue;
isCut = false;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && isCut)
{
storeTran.GetChild(2).gameObject.SetActive(false);
storeTran.GetChild(1).gameObject.SetActive(true);
timer = timerValue;
isCut = false;
}
}
}
7、游戏管理GamaMagager脚本
using UnityEngine;
public class GamaMagager : MonoBehaviour
{
public static GamaMagager Instance;
public AudioConfig audioConfig;
void Start()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
}
8、Sound manager script
using UnityEngine;
public class AudioManager : Single<AudioManager>
{
public AudioSource source;
private void Start()
{
source = GetComponent<AudioSource>();
}
public void playAudio(AudioClip clip)
{
source.PlayOneShot(clip);
}
public void pauseAudio(AudioClip clip)
{
source.Pause();
}
}
9、Sound visualization data managerScriptableObject
using UnityEngine;
[CreateAssetMenu(fileName = "AudioConfig", menuName = "AudioConfig")]
public class AudioConfig : ScriptableObject
{
public AudioClip shot;
public AudioClip hit the sound;
public AudioClip 跳;
public AudioClip 加特林;
}
边栏推荐
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- (八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
- Recursive implementation of the Tower of Hanoi problem
- 【SemiDrive源码分析】【MailBox核间通信】44 - 基于Mailbox IPCC RPC 实现核间通信(RTOS侧 IPCC_RPC Server 消息接收及回复 原理分析篇)
- Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
- $parent/$children and ref
- ClickHouse:设置远程连接
- 【wpf】wpf中的那些模板之深度解析
- [shell basics] determine whether the directory is empty
- MySQL数据库增删改查(基础操作命令详解)
猜你喜欢

Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us

重磅 | 开放原子校源行活动正式启动

The idea project obviously has dependencies, but the file is not displayed, Cannot resolve symbol 'XXX'

idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘

【wpf】wpf中的那些模板之深度解析

Why don't you programmers make a living off your own projects?And have to work for someone else?

马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远

Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined

Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry

How Zotero removes auto-generated tags
随机推荐
Safety 20220718
unity2d小游戏
The BP neural network
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
重磅 | 开放原子校源行活动正式启动
(线段树) 基础线段树常见问题总结
C# 实现PLC的定时器
(tree) Last Common Ancestor (LCA)
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
Pytest电商项目实战(上)
Bubble sort, selection sort, insertion sort, binary search directly
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
Port inspection steps - 7680 port analysis - Dosvc service
BUG消灭者!!实用调试技巧超全整理
qlib自动化quant
《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
Unity打灵狐者
C语言表白代码?
"DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction" paper notes
(6) Enumeration and annotation