当前位置:网站首页>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 加特林;
}
边栏推荐
- Thinking about data governance after Didi fines
- C language confession code?
- 递归实现汉诺塔问题
- el-image tag doesn't work after binding click event
- MySQL基础操作
- C语言表白代码?
- SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
- ENSP,划分VLAN、静态路由,三层交换机综合配置
- ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
猜你喜欢
How Zotero removes auto-generated tags
No qualifying bean of type question
MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
【论文阅读】Mastering the game of Go with deep neural networks and tree search
(五)final、抽象类、接口、内部类
扫雷小游戏——C语言
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry
Vue项目通过node连接MySQL数据库并实现增删改查操作
随机推荐
关于出现大量close_wait状态的理解
No qualifying bean of type question
微信小程序使用云函数更新和添加云数据库嵌套数组元素
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
MySQL fuzzy query can use INSTR instead of LIKE
HCIP Day 10_BGP Route Summary Experiment
npm、nrm两种方式查看源和切换镜像
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
Basic knowledge of mysql (2)
IDEA常用快捷键与插件
VScode+ESP32 quickly install ESP-IDF plugin
扫雷小游戏——C语言
开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
Recursive implementation of the Tower of Hanoi problem
重磅 | 开放原子校源行活动正式启动
BUG消灭者!!实用调试技巧超全整理
ClickHouse:设置远程连接
log level and print log note
(树) 最近公共祖先(LCA)
WeChat applet uses cloud functions to update and add cloud database nested array elements