Unity3d即时战斗之敌人AI和角色攻击

本文深入探讨Unity3D中的即时战斗系统,重点讲解敌人AI的实现和角色控制脚本Character.cs,阐述如何为游戏角色添加智能控制器,以及战斗过程中的攻击机制,使用C#进行编程。
摘要由CSDN通过智能技术生成

角色控制脚本Character.cs,为角色添加角色控制器Character controller。

<span style="font-size:14px;">using UnityEngine;
using System.Collections;

public class Character : MonoBehaviour {

	float attackInterval = 1.5f;
	float timer = 0;
	public float speed = 5.0F;
	public float jumpSpeed = 10.0F;
	public float gravity = 20.0F;
	private Vector3 moveDirection = Vector3.zero;
	private Animator anim;
	private CharacterController controller;
	public Transform cameraTarget;
	public GameObject effect1;
	public GameObject effect2;
	public GameObject effect3;
	public GameObject AttackObj;
	float Rot_y;
	Health heroHealth;
	Enemy enemy;
	private bool isRun = false;
	private bool isAttack = false;
	private bool isDeath = false;

	void Start () {
		heroHealth = GetComponent<Health> ();
		anim = GetComponent<Animator> ();
		controller = GetComponent<CharacterController> ();

	}

	// Update is called once per frame
	void Update () {

		if (heroHealth.currentheroHelth > 0) {
			if (!isAttack) {
				Move ();
			}else
				Attack ();
		} else if(!isDeath){
			anim.SetTrigger ("Death");
			isDeath = true;
		}

	}
	void OnTriggerEnter(Collider col){
		if (col.gameObject.tag == "Monster") {
			isAttack = true;
			anim.SetBool ("isAttack", true);
			AttackObj = col.gameObject;
			enemy = AttackObj.GetComponent<Enemy> ();
		}
	}
	void OnTriggerExit(Collider col){
		if (col.gameObject.tag == "Monster") {
			isAttack = false;
			anim.SetBool("isAttack",false);
			AttackObj = null;
		}
	}
	void Move(){
		if (Input.GetKeyDown (KeyCode.LeftShift)) {
			if (!isRun) {
				isRun = true;
				anim.SetBool ("isRun", true);
			} else {
				isRun = false;
				anim.SetBool ("isRun", false);
			}
		}

		if (controller.isGrounded) {

			if (Input.GetButtonDown ("Jump")) {
				moveDirection.y = jumpSpeed;
			}
		}
		moveDirection.y -= gravity * Time.deltaTime;
		controller.Move( moveDirection * Time.deltaTime);

		if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.S)) {
			anim.SetInteger (&
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值