006_2D Ruby 项目脚本
CogBulletController.cs
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);
}
}
DamageZone.cs
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);
}
}
}
EnemyController.cs
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;
public ParticleSystem smokeEffect;
bool repaired;
AudioSource audioSource;
private void Start () {
rigidbody2D = GetComponent<Rigidbody2D>();
moveTimer = moveTime;
animator = GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
}
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" );
smokeEffect.Stop();
audioSource.Stop();
}
}
HealthCollectible.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthCollectible : MonoBehaviour
{
public AudioClip collectedClip;
public float soundVol = 1.0f ;
public int amount = 1 ;
int collideCount;
private void OnTriggerEnter2D (Collider2D collision)
{
collideCount++;
Debug.Log($"和当前物体发生碰撞的对象是:{collision},当前是第{collideCount}次碰撞!" );
RubyController rubyController = collision.GetComponent<RubyController>();
if (rubyController != null)
{
Debug.Log($"ruby health is {rubyController.health}" );
if (rubyController.health < rubyController.maxHealth)
{
rubyController.ChangeHealth(amount);
Destroy(gameObject);
rubyController.PlaySound(collectedClip, soundVol);
}
else
{
Debug.Log("当前玩家角色生命是满的,不需要加血" );
}
}
else
{
Debug.LogError("rubyController游戏组件并未获取到" );
}
}
}
NonPlayerCharacter.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class NonPlayerCharacter : MonoBehaviour {
public float displayTime = 4.0f ;
public GameObject dialogBox;
float displayTimer;
public GameObject tmpGameObject;
TextMeshProUGUI tmpTextBox;
int currentPage = 1 ;
int totalPages;
void Start () {
dialogBox.SetActive(false );
displayTimer = -1.0f ;
tmpTextBox = tmpGameObject.GetComponent<TextMeshProUGUI>();
}
void Update () {
if (displayTimer >= 0.0f ) {
if (Input.GetKeyDown(KeyCode.Space)) {
if (currentPage < totalPages) {
currentPage++;
} else {
currentPage = 1 ;
}
tmpTextBox.pageToDisplay = currentPage;
}
displayTimer -= Time.deltaTime;
} else {
dialogBox.SetActive(false );
}
}
public void DisplayDialog () {
totalPages = tmpTextBox.textInfo.pageCount;
displayTimer = displayTime;
dialogBox.SetActive(true );
}
}
RubyController.cs
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 {
AudioSource audioSource;
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>();
audioSource = GetComponent<AudioSource>();
}
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();
}
if (Input.GetKeyDown(KeyCode.E)) {
RaycastHit2D hit = Physics2D.Raycast(rigidbody2D.position + Vector2.up * 0.2f , move, 1.5f , LayerMask.GetMask("NPC" ));
if (hit.collider != null) {
Debug.Log($"射线投射碰撞到的对象是:{hit.collider.gameObject}" );
NonPlayerCharacter npc = hit.collider.GetComponent<NonPlayerCharacter>();
if (npc != null) {
npc.DisplayDialog();
}
}
}
}
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);
UIHealthBar.instance.SetValue(currentHealth/(float )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" );
}
public void PlaySound (AudioClip audioClip, float soundVol) {
audioSource.PlayOneShot(audioClip, soundVol);
}
}
UIHealthBar.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIHealthBar : MonoBehaviour {
public static UIHealthBar instance { get; private set ; }
public Image mask;
float originalSize;
void Awake () {
instance = this;
}
void Start () {
originalSize = mask.rectTransform.rect.width;
}
void Update () {
}
public void SetValue (float value) {
mask.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, originalSize*value);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!