005_添加动画、子弹碰撞、无敌时间
RubyController
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.UIElements;
using UnityEngine.WSA;
public class RubyController : MonoBehaviour {
public GameObject cogBulletPrefab;
public float invincibleTime = 2.0f;
public float invincibleTimer;
bool isInvincible;
public int maxHealth = 5;
public int health {
get { return currentHealth; }
}
int currentHealth;
public float speed = 4f;
Rigidbody2D rigidbody2D;
public float horizontal = 0f;
public float vertical = 0f;
Animator animator;
Vector2 move;
private void Start() {
rigidbody2D = GetComponent<Rigidbody2D>();
currentHealth = 3;
Debug.Log($"currentHealth:{currentHealth}");
move = new Vector2(1, 0);
animator = GetComponent<Animator>();
}
void Update() {
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
if (!Mathf.Approximately(horizontal, 0f) || !Mathf.Approximately(vertical, 0f)) {
move = new Vector2(horizontal, vertical);
move.Normalize();
animator.SetFloat("Look X", move.x);
animator.SetFloat("Look Y", move.y);
animator.SetFloat("Speed", move.magnitude);
} else {
animator.SetFloat("Speed", 0);
}
if (isInvincible) {
invincibleTimer -= Time.deltaTime;
if (invincibleTimer < 0) {
isInvincible = false;
}
}
if(Input.GetKeyDown(KeyCode.LeftShift)) {
speed = 8f;
} else if(Input.GetKeyUp(KeyCode.LeftShift)) {
speed = 4f;
}
if (Input.GetMouseButtonDown(0)) {
Launch();
}
}
private void FixedUpdate() {
Vector2 position = rigidbody2D.position;
position.x = position.x + speed * horizontal * Time.deltaTime;
position.y = position.y + speed * vertical * Time.deltaTime;
rigidbody2D.position = position;
}
public void ChangeHealth(int amount) {
if (amount < 0) {
animator.SetTrigger("Hit");
if (isInvincible) {
return;
}
isInvincible = true;
invincibleTimer = invincibleTime;
}
currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
Debug.Log("当前生命值:" + currentHealth + "/" + maxHealth);
}
private void Launch() {
GameObject cogBulletObject = Instantiate(cogBulletPrefab, rigidbody2D.position + Vector2.up * 0.5f, Quaternion.identity);
CogBulletController cogBullet = cogBulletObject.GetComponent<CogBulletController>();
cogBullet.Launch(move, 600);
animator.SetTrigger("Launch");
}
}
EnemyController
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class EnemyController : MonoBehaviour {
public int damageNum = -1;
public float speed = 3f;
Rigidbody2D rigidbody2D;
public float horizontal = 0;
public float vertical = 1;
public float moveTime = 3;
float moveTimer;
Animator animator;
bool repaired;
private void Start() {
rigidbody2D = GetComponent<Rigidbody2D>();
moveTimer = moveTime;
animator = GetComponent<Animator>();
}
private void Update() {
if (repaired) {
return;
}
moveTimer -= Time.deltaTime;
if (moveTimer < 0) {
vertical *= -1;
horizontal *= -1;
moveTimer = moveTime;
}
}
private void FixedUpdate() {
if (repaired) {
return;
}
Vector2 position = rigidbody2D.position;
position.x = position.x + speed * horizontal * Time.deltaTime;
position.y = position.y + speed * vertical * Time.deltaTime;
rigidbody2D.position = position;
animator.SetFloat("Move X", position.x);
animator.SetFloat("Move Y", position.y);
}
private void OnCollisionEnter2D(Collision2D collision) {
RubyController rubyController = collision.gameObject.GetComponent<RubyController>();
if (rubyController != null) {
rubyController.ChangeHealth(damageNum);
}
}
public void Fix() {
repaired = true;
rigidbody2D.simulated = false;
animator.SetTrigger("Fixed");
}
}
CogBulletController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CogBulletController : MonoBehaviour {
Rigidbody2D rigidbody2D;
void Start() {
}
private void Awake() {
rigidbody2D = GetComponent<Rigidbody2D>();
}
void Update() {
if(transform.position.magnitude > 100f) {
Destroy(gameObject);
}
}
public void Launch(Vector2 direction, float force) {
rigidbody2D.AddForce(direction * force);
}
private void OnCollisionEnter2D(Collision2D collision) {
EnemyController enemyController = collision.gameObject.GetComponent<EnemyController>();
if (enemyController != null) {
enemyController.Fix();
}
Debug.Log($"齿轮子弹碰撞到了: {collision.gameObject}");
Destroy(gameObject);
}
}
DamageeZone
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageZone : MonoBehaviour
{
public int damageNum = -1;
private void OnTriggerStay2D(Collider2D collision)
{
RubyController rubyController = collision.gameObject.GetComponent<RubyController>();
if(rubyController != null )
{
rubyController.ChangeHealth(damageNum);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!