当前位置:网站首页>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 加特林;
}
边栏推荐
- RESTful api interface design specification
- Redis uses sorted set to cache latest comments
- ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
- type_traits元编程库学习
- C language confession code?
- 强化学习:从入门到入坑再到拉屎
- ClickHouse:设置远程连接
- MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)
- MySQL数据库增删改查(基础操作命令详解)
- 【wpf】wpf中的那些模板之深度解析
猜你喜欢

MySQL based operations

递归实现汉诺塔问题

C语言表白代码?

Win10 CUDA CUDNN 安装配置(torch paddlepaddle)

【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
![Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]](/img/83/e0163b324448c6ef5b106862673637.jpg)
Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]

BUG消灭者!!实用调试技巧超全整理

C language from entry to such as soil, the data store

Win10 CUDA CUDNN installation configuration (torch paddlepaddle)

ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
随机推荐
$parent/$children and ref
(tree) Last Common Ancestor (LCA)
[Swift] Customize the shortcut that pops up by clicking the APP icon
Pytest e-commerce project combat (on)
BP神经网络
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
ENSP,划分VLAN、静态路由,三层交换机综合配置
Redis uses LIST to cache the latest comments
Unity打灵狐者
(六)枚举、注解
log level and print log note
pom文件成橘红色未加载的解决方案
Daily practice of LeetCode - palindrome structure of OR36 linked list
两个地址池r2负责管地址池r1负责管dhcp中继
Redis uses sorted set to cache latest comments
MySQL数据库必会的增删查改操作(CRUD)
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
Smartcom Programming Level 4 - Magic Academy Lesson 6
MySQL based operations
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your