`
duoerbasilu
  • 浏览: 1516229 次
文章分类
社区版块
存档分类
最新评论

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);
			}
		}
	}
}


分享到:
评论

相关推荐

    PlayerAttack_C#_

    【PlayerAttack_C#_】项目概述 在"PlayerAttack_C#_"这个项目中,我们可以看到一个基于C#语言的小型代码示例,很可能是用于游戏开发或者某种模拟环境中的玩家攻击逻辑。C#是一种广泛应用于游戏开发、桌面应用以及...

    类和对象的具体介绍

    public void attacked(int _playerattack) { hp -= _playerattack; if (hp ) hp = 0; Console.WriteLine("Monster is killing"); } } class MainClass { public static void Main(string[] args) { ...

    PRO-C34-KILL-THE-MONSTER

    let damage = playerAttack - monster.defense; // 假设怪物有防御值 monster.health -= damage; if (monster.health ) { console.log('你成功杀死了怪物!'); } else { console.log(`怪物还剩下 ${monster....

    c-34_killTheMonster

    function attack(monster, playerAttack) { monster.health -= playerAttack; if (monster.health ) { killMonster(monster); } } function killMonster(monster) { console.log('怪物已死亡'); // 更新UI,...

    Project34:杀死怪物

    function playerAttack() { let damage = Math.floor(Math.random() * 50) + 1; // 随机伤害 let effectiveDamage = damage - monster.defense; if (effectiveDamage ) { effectiveDamage = 0; } monster.HP -...

    UnityRPGActionCommands:项目灵魂

    在RPG中,可能会有事件如"PlayerAttack"或"MonsterDie",这些事件可以通过委托和事件处理器来触发和响应。 7. **动画系统**:Unity 提供了强大的Animator组件和Mecanim系统,用于控制角色和物体的动画。在动作命令...

    战舰

    public void PlayerAttack(int row, int col) { // 检查并处理攻击 } public void AIAttack() { // 实现AI的攻击策略 } ``` 此外,为了增加游戏的趣味性和挑战性,可以实现AI的智能算法,比如随机攻击、基于概率...

Global site tag (gtag.js) - Google Analytics