当前位置:网站首页>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 加特林;
}
边栏推荐
- 端口排查步骤-7680端口分析-Dosvc服务
- 手把手实现图片预览插件(三)
- C语言表白代码?
- visual studio 那些提高效率的快捷键,总结(不时更新)
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- Bubble sort, selection sort, insertion sort, binary search directly
- (Line segment tree) Summary of common problems of basic line segment tree
- (五)final、抽象类、接口、内部类
- 开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
- Daily practice of LeetCode - 138. Copy a linked list with random pointers
猜你喜欢
"A daily practice, happy water problem" 1331. Array serial number conversion
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
BP神经网络
LocalDate addition and subtraction operations and comparison size
exsl文件预览,word文件预览网页方法
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
Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
Unity打灵狐者
扫雷游戏(c语言写)
随机推荐
(线段树) 基础线段树常见问题总结
log level and print log note
[shell basics] determine whether the directory is empty
input输入框展示两位小数之precision
errno error code and meaning (Chinese)
Component pass value provide/inject
(六)枚举、注解
$parent/$children and ref
重磅 | 开放原子校源行活动正式启动
(5) final, abstract class, interface, inner class
手把手实现图片预览插件(三)
Redis uses sorted set to cache latest comments
BP神经网络
IDEA常用快捷键与插件
SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
$attrs/$listeners
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
RESTful api接口设计规范
MySQL模糊查询可以使用INSTR替代LIKE
C# 实现PLC的定时器