PlayerAttack

using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour {
	public GameObject target;
	public float attackTimer;
	public float coolDown;
	
	// Use this for initialization
	void Start () {
		attackTimer = 0;
		coolDown = 2.0f;
	}
	
	// Update is called once per frame
	void Update () {
		// Edge check
		if (attackTimer > 0)
			attackTimer -= Time.deltaTime;
		
		if (attackTimer < 0)
			attackTimer = 0;
		
		if (Input.GetKeyUp(KeyCode.F)) {
			// Limit the rate of attack
			if (attackTimer == 0) {
				Attack();
				attackTimer = coolDown;
			}
		}
	}
	
	private void Attack () {
		// If two objects is the same direction and close enough, then enable damage.
		float distance = Vector3.Distance(target.transform.position, transform.position);
		
		Vector3 dir = (target.transform.position - transform.position).normalized;
		
		float direction = Vector3.Dot(dir, transform.forward);
		
		if (distance < 2.5f) {
			if (direction > 0) {
				EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
				eh.AddjustCurrentHealth(-10);
			}
		}
	}
}


using System.Collections; using TMPro; using UnityEngine; public enum Battle { Start, PlayerTurn, EnemyTurn, Lose, Win } public class BattleSystem : MonoBehaviour { public Battle State; public float typingSpeed = 0.05f; private bool isTyping = false; public GameObject PlayerPrefeb; public GameObject EnemyPrefeb; public Transform PlayerPosition; public Transform EnemyPosition; private Playermessage playerpeople; private Enemymessage emenypeople; public EnemyBattleHUD enemyHUD; public PlayerBattleHUD playerHUD; public TextMeshProUGUI Text; // 用于显示对话的文本组件 public GameObject F; public GameObject m; void Start() { State = Battle.Start; StartCoroutine(SetupBattle()); } private IEnumerator SetupBattle() { GameObject player = Instantiate(PlayerPrefeb, PlayerPosition); playerpeople = player.GetComponent<Playermessage>(); GameObject emeny = Instantiate(EnemyPrefeb, EnemyPosition); emenypeople = emeny.GetComponent<Enemymessage>(); playerHUD.SetHUD(playerpeople); enemyHUD.SetHUD(emenypeople); // 显示开场信息(带打字效果) yield return StartCoroutine(TypeText("Let me feel your power now, don't make me bored^_^", 0.5f)); yield return new WaitForSeconds(0.5f); if (F != null) F.SetActive(false); if (m != null) m.SetActive(true); yield return StartCoroutine(TypeText("I can't lose here", 0.5f)); yield return new WaitForSeconds(0.5f); if (m != null) m.SetActive(false); yield return StartCoroutine(TypeText("--Please consider your decision--", 0.5f)); State = Battle.PlayerTurn; PlayerTurn(); } // 玩家回合逻辑 private void PlayerTurn() { StartCoroutine(TypeText("Choose your action...", 0f)); } public void OnAttackButton() { if (State != Battle.PlayerTurn) return; StartCoroutine(PlayerAttack()); } private IEnumerator PlayerAttack() { yield return StartCoroutine(TypeText("Get out of my way!!!", 0f)); yield return new WaitForSeconds(1f); bool IsDead = emenypeople.TakeDamage(playerpeople.atk, emenypeople.def); enemyHUD.SetHUD(emenypeople); // 显示伤害数值 int damageDealt = Mathf.Max(playerpeople.atk - emenypeople.def, 100); yield return StartCoroutine(TypeText($"Dealt {damageDealt} damage!", 0f)); yield return new WaitForSeconds(1f); if (IsDead) { State = Battle.Win; EndBattle(); } else { State = Battle.EnemyTurn; StartCoroutine(EnemyTurn()); } } private IEnumerator EnemyTurn() { yield return StartCoroutine(TypeText("Look into my eyes...", 0f)); yield return new WaitForSeconds(1f); bool IsDead = playerpeople.TakeDamage(emenypeople.atk, playerpeople.def); playerHUD.SetHUD(playerpeople); int damageDealt = Mathf.Max(emenypeople.atk - playerpeople.def, 300); yield return StartCoroutine(TypeText($"Took {damageDealt} damage!", 0f)); yield return new WaitForSeconds(1f); if (IsDead) { State = Battle.Lose; EndBattle(); } else { State = Battle.PlayerTurn; PlayerTurn(); } } private void EndBattle() { if (State == Battle.Win) { StartCoroutine(TypeText("Victory is mine!", 0f)); } else if (State == Battle.Lose) { StartCoroutine(TypeText("Defeat... I'll be back!", 0f)); } } // 打字效果协程 public IEnumerator TypeText(string sentence, float initialDelay = 0f) { if (Text == null) yield break; isTyping = true; yield return new WaitForSeconds(initialDelay); Text.text = ""; foreach (char letter in sentence.ToCharArray()) { Text.text += letter; yield return new WaitForSeconds(typingSpeed); } isTyping = false; } }为什么 int damageDealt = Mathf.Max(emenypeople.atk - playerpeople.def, 300);中改数字滑动条没变化
07-25
using System.Collections; using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.SceneManagement; public class BattleSystem : MonoBehaviour { public enum BattleState { Start, PlayerTurn, EnemyTurn, Win, Lose } public BattleState state; // 角色引用 public GameObject playerPrefab; public GameObject enemyPrefab; public Transform playerBattleStation; public Transform enemyBattleStation; private Playermessage playerUnit; private Enemymessage enemyUnit; // UI组件 public PlayerBattleHUD playerHUD; public EnemyBattleHUD enemyHUD; public TMP_Text dialogueText; // 战斗效果 public Image attackphoto; public Image attackphoto2; public Image image; public Animator animator; public Camera mainCamera; public Image image1; // 颜色变量 private Color Originalcolor; // 战斗开始 void Start() { state = BattleState.Start; StartCoroutine(SetupBattle()); } // 初始化战斗 IEnumerator SetupBattle() { // 实例化角色 GameObject playerGO = Instantiate(playerPrefab, playerBattleStation); playerUnit = playerGO.GetComponent<Playermessage>(); GameObject enemyGO = Instantiate(enemyPrefab, enemyBattleStation); enemyUnit = enemyGO.GetComponent<Enemymessage>(); // 设置HUD playerHUD.SetHUD(playerUnit); enemyHUD.SetHUD(enemyUnit); // 开场对话 yield return StartCoroutine(TypeText($"Let me feel your power now, don't make me bored^_^", 0.05f)); yield return new WaitForSeconds(0.5f); image1.gameObject.SetActive(false); // 省略部分对话... yield return StartCoroutine(TypeText("--Please consider your decision--", 0.05f)); yield return new WaitForSeconds(0.5f); state = BattleState.PlayerTurn; PlayerTurn(); } // 玩家回合 private void PlayerTurn() { StartCoroutine(TypeText("Choose your action...", 0f)); } // 玩家攻击 public void OnAttackButton() { if (state != BattleState.PlayerTurn) return; StartCoroutine(PlayerAttack()); } // 玩家攻击协程 private IEnumerator PlayerAttack() { yield return StartCoroutine(TypeText("Get out of my way!!!", 0f)); yield return new WaitForSeconds(1f); // 显示攻击特效 attackphoto.gameObject.SetActive(true); yield return new WaitForSeconds(0.2f); // 计算伤害 float damageDealt = playerUnit.CalculateDamage(); // 显示伤害文本 yield return StartCoroutine(TypeText($"Dealt {damageDealt:F1} damage!", 0f)); yield return new WaitForSeconds(1f); // 隐藏攻击特效 attackphoto.gameObject.SetActive(false); // 敌人承受伤害 bool isDead = enemyUnit.TakeDamage(damageDealt); enemyHUD.SetHP(enemyUnit.CurrentHealth); if (isDead) { state = BattleState.Win; StartCoroutine(EndBattle()); } else { state = BattleState.EnemyTurn; StartCoroutine(EnemyTurn()); } } // 敌人回合 private IEnumerator EnemyTurn() { yield return StartCoroutine(TypeText("Looking my eyes...", 0f)); yield return new WaitForSeconds(0.5f); // 显示攻击特效 attackphoto2.gameObject.SetActive(true); yield return new WaitForSeconds(0.2f); // 屏幕震动效果 animator.SetTrigger("shake"); StartCoroutine(ShakeCamera(0.4f, 0.4f)); // 闪光效果 Originalcolor = image.color; StartCoroutine(FlashBoom()); // 计算伤害 float damageDealt = enemyUnit.CalculateDamage(); yield return StartCoroutine(TypeText($"Took {damageDealt:F1} damage!", 0f)); // 隐藏攻击特效 attackphoto2.gameObject.SetActive(false); yield return new WaitForSeconds(0.5f); // 玩家承受伤害 bool isDead = playerUnit.TakeDamage(damageDealt); playerHUD.SetHP(playerUnit.CurrentHealth); yield return new WaitForSeconds(1f); if (isDead) { state = BattleState.Lose; StartCoroutine(EndBattle()); } else { state = BattleState.PlayerTurn; PlayerTurn(); } } // 结束战斗 private IEnumerator EndBattle() { if (state == BattleState.Win) { yield return StartCoroutine(TypeText("Victory is mine!", 0f)); // 保存胜利结果 PlayerPrefs.SetInt("LastBattleResult", 1); } else if (state == BattleState.Lose) { yield return StartCoroutine(TypeText("Defeat... I'll be back!", 0f)); // 保存失败结果 PlayerPrefs.SetInt("LastBattleResult", 0); } // 添加场景切换 SceneManager.LoadScene("Traning"); } // 文本显示效果 IEnumerator TypeText(string text, float delay) { dialogueText.text = ""; foreach (char letter in text.ToCharArray()) { dialogueText.text += letter; yield return new WaitForSeconds(delay); } } // 相机震动效果 IEnumerator ShakeCamera(float duration, float magnitude) { Vector3 originalPos = mainCamera.transform.localPosition; float elapsed = 0f; while (elapsed < duration) { float x = Random.Range(-1f, 1f) * magnitude; float y = Random.Range(-1f, 1f) * magnitude; mainCamera.transform.localPosition = new Vector3(x, y, originalPos.z); elapsed += Time.deltaTime; yield return null; } mainCamera.transform.localPosition = originalPos; } // 闪光效果 IEnumerator FlashBoom() { image.color = Color.white; yield return new WaitForSeconds(0.1f); image.color = Originalcolor; } } 我不想一开始就进入战斗而是在触发器内使用F键开始对话
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值