当前位置:网站首页>unity官方案例RubyAdventure
unity官方案例RubyAdventure
2022-07-13 18:01:00 【MaNongSa】
提示:以下是本篇文章正文内容
Build分享
百度网盘:
链接:https://pan.baidu.com/s/1WkQbKPhD4hpvxqsPHiLFtw
提取码:6666
效果图

代码:
player
Rigidbody2D rigidbody2d;
private Animator animator;
public GameObject bullet;
private GameObject bulletObj;
private int bulletValue;
private int bulletMaxValue=99;
public int BulletValue { get => bulletValue; set => bulletValue = value; }
public int BulletMaxValue { get => bulletMaxValue; set => bulletMaxValue = value; }
public AudioClip audio;
private float horizontal;
private float vertical;
public float timeInvincible = 2.0f;
public float speed = 3.0f;
private float invincibleTimer;
private int maxHealth = 5;
public int MaxHealth { get => maxHealth; set => maxHealth = value; }
private int currentHealth;
public int CurrentHealth { get => currentHealth;
set{
currentHealth = value;
}
}
private bool isInvincible;
private Vector2 lookDirection = new Vector2(1, 0);
private Vector2 move;
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
Application.targetFrameRate = 60;
currentHealth = MaxHealth;
BulletValue = 10;
UIHealthBar.Instance.UpdateBullet(BulletValue, BulletMaxValue);
}
void Update()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
move = new Vector2(horizontal, vertical);
if (!Mathf.Approximately(move.x, 0) || !Mathf.Approximately(move.y, 0))
{
lookDirection = new Vector2(move.x, move.y);
lookDirection.Normalize();
}
animator.SetFloat("Look X", lookDirection.x);
animator.SetFloat("Look Y", lookDirection.y);
animator.SetFloat("Speed", move.magnitude);
if (invincibleTimer > 0)
{
invincibleTimer -= Time.deltaTime;
}
else {
isInvincible = false;
}
if (Input.GetKeyDown(KeyCode.J)&& BulletValue>0)
{
Launch();
BulletValue--;
UIHealthBar.Instance.UpdateBullet(BulletValue, BulletMaxValue);
}
if (Input.GetKeyDown(KeyCode.E))
{
RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask("NPC"));
if (hit.collider != null)
{
NPC nPC = hit.collider.GetComponent<NPC>();
if (nPC != null)
{
nPC.showNPC();
}
}
Debug.DrawLine(rigidbody2d.position + Vector2.up * 0.2f, hit.point, Color.red);
}
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
}
void FixedUpdate()
{
//第二次变动
Vector2 position = rigidbody2d.position;
position.x = position.x + 3.0f * horizontal * Time.deltaTime* speed;
position.y = position.y + 3.0f * vertical * Time.deltaTime* speed;
if (!AudioManager.Instance.source.isPlaying&& horizontal!=0)
{
//播放
AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.PlayerMove声音);
}
rigidbody2d.MovePosition(position);
}
public void ChangeHealth(int amount)
{
if (amount < 0)
{
if (isInvincible)
return;
isInvincible = true;
invincibleTimer = timeInvincible;
animator.SetBool("Hit", true);
//播放
AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.受伤声音);
}
//返回最小到最大之间的值=Mathf.Clamp(当前的值,最小值,最大值)
CurrentHealth = Mathf.Clamp(CurrentHealth + amount, 0, MaxHealth);
UIHealthBar.Instance.SetValue(CurrentHealth, MaxHealth);
Debug.Log(CurrentHealth + "/" + MaxHealth);
if (CurrentHealth == 0)
{
Die();
}
}
/// <summary>
/// 发射炮弹
/// </summary>
void Launch()
{
//播放
AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.攻击声音);
bulletObj = Instantiate(bullet, rigidbody2d.position + Vector2.up * 1f, Quaternion.identity);
Projectile projectile = bulletObj.GetComponent<Projectile>();
projectile.Launch(lookDirection, 500);
animator.SetTrigger("Launch");
}
/// <summary>
/// 捡到子弹包舔加子弹
/// </summary>
public void bulletBagAddFun(int BulletValueF)
{
//播放
AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.捡到金币声音);
BulletValue += BulletValueF;
UIHealthBar.Instance.UpdateBullet(BulletValue, BulletMaxValue);
}
private void Die()
{
Debug.Log("asd");
SceneManager.LoadScene("SampleScene");
}
enemy
private Rigidbody2D rigidbody2d;
//特效
public ParticleSystem smokeEffect;
Animator animator;
private AudioSource source;
public bool vertical;
private bool broken=true;
public float speed=2;
public float changeTime = 3.0f;
float timer;
int direction = 1;
// 在第一次帧更新之前调用 Start
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
timer = changeTime;
animator = GetComponent<Animator>();
source = GetComponent<AudioSource>();
}
void Update()
{
timer -= Time.deltaTime;
if (timer < 0)
{
direction = -direction;
timer = changeTime;
}
}
void FixedUpdate()
{
Vector2 position = rigidbody2d.position;
if (vertical)
{
position.y = position.y + Time.fixedDeltaTime * speed * direction;
animator.SetFloat("movX",0 );
animator.SetFloat("movY", direction);
}
else
{
position.x = position.x + Time.fixedDeltaTime * speed * direction;
animator.SetFloat("movX", direction);
animator.SetFloat("movY", 0);
}
//移动
rigidbody2d.MovePosition(position);
if (!broken)
{
return;
}
}
//碰撞器
void OnCollisionEnter2D(Collision2D other)
{
RubyController player = other.gameObject.GetComponent<RubyController>();
if (player != null)
{
player.ChangeHealth(-1);
}
}
//使用 public 的原因是我们希望像飞弹脚本一样在其他地方调用这个函数
public void Fix()
{
broken = false;
rigidbody2d.simulated = false;
//如果你添加了修复动画,则为可选
smokeEffect.Stop();
source.Pause();
//播放声音
AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.维修机器人修复声音);
animator.SetTrigger("fieed");
胜利关.Instance.i++;
}
qw
NPC(青蛙先生)——q’w
public GameObject E;
public GameObject dialogBox;
private float timer;
private float showTextTimer=4f;
private float currentTimer;
void Start()
{
E.SetActive(true);
dialogBox.SetActive(false);
currentTimer = -1f;
}
void Update()
{
if (currentTimer > 0)
{
currentTimer -= Time.deltaTime;
if (currentTimer < 0)
{
dialogBox.SetActive(false);
E.SetActive(true);
timer = 0;
currentTimer = -1f;
}
}
}
public void showNPC()
{
currentTimer = showTextTimer;
dialogBox.SetActive(true);
E.SetActive(false);
}
总结
利用碰撞器或者触发器然后通过触发对象来拿到对应的脚本,进行判断然后调用;
通过rigidbody2d.MovePosition(position)移动可以放置颤抖
边栏推荐
- 散列表HashTable线性探测法类模板的实现
- 动态规划+组合数学
- SSM integration (classic self version)
- DDD——在我梦里,我还能让你把我给欺负了?
- 技术管理进阶——什么是影响力
- Use the pointer to write a program to insert a character at any position of a string (the position of the inserted character is required to be input by the user from the keyboard).
- Pat's notes on question brushing [collation of English expressions of unknown key points in the title]
- CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
- LeetCode精讲——1252. 奇数值单元格的数目(难度:简单)
- LeetCode精讲——676. 实现一个魔法字典(难度:中等)
猜你喜欢

ThreadLocal源码解析

Comparison and application of DHT11 and dht22 (am2302)

Chapter VI use of OLED module +stm32

Leatcode Speech - 676. Implémenter un dictionnaire magique (difficulté: moyenne)

KMP中的最小循环节

Use of rounding, rounding down, rounding up

Small stage summary

Rtthread creation of thread

Leetcode lecture - 735 Planetary collision (difficulty: medium)

RT_ Thread producer and consumer issues
随机推荐
Dark horse database notes DCL
多图详解阻塞队列——SynchronousQueue
SSM integration (classic self version)
C language to realize Hanoi Tower (detailed explanation of program execution steps)
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
Redrawing
Go秒杀系统3--项目结构搭建,商品模型开发。
Amd rDNA 3 Navi 31 flagship GPU is said to be packaged in 3D v-cache and up to 384mb cache
LeetCode 刷题 第四天
散列表HashTable线性探测法类模板的实现
KMP中的最小循环节
黑马数据库知识
Simple thread example - running Lantern - stack space allocation skills
Small stage summary
DL第五天
LeetCode 刷题 第十天
[introduction to go language] 05 go language branch statement
DL第六天
IO多路复用
交易模块开发