标签:blog io os ar for sp 文件 div on
1. 首先是人物的挂载文件
// 定义变量
private Transform AttackTarget = null; // 攻击目标 private Vector2 MoveDistance = new Vector2 (5, 8); //攻击后向后的移动距离 private Vector2 MoveDirector = new Vector2 (-60, 60);// 攻击后相对目标的移动角度 bool moveFlag = false; // 是否移动控制变量 Vector3 m_refFix = Vector3.zero; Vector3 moveToPos = Vector3.zero; // 计算后的移动位置 float m_refcomSmoothTime = 2.0f; // 移动所需要的时间
// 对外接口1 攻击目标的设定
public void SetTargetTrans(Transform target) { this.AttackTarget = target; }
// 攻击后移动的位置设置
void setMosterMoveTo () { if (this.AttackTarget == null) return; float distance = UnityEngine.Random.Range (MoveDistance.x, MoveDistance.y); float angle = UnityEngine.Random.Range (MoveDirector.x, MoveDirector.y); Quaternion r= AttackTarget.rotation; Quaternion r0= Quaternion.Euler(AttackTarget.rotation.eulerAngles.x,AttackTarget.rotation.eulerAngles.y + angle, AttackTarget.rotation.eulerAngles.z); Vector3 movePos = (AttackTarget.position + (r0 *Vector3.forward) * distance); if(Vector3.Distance(transform.position , AttackTarget.position) < 20 ) { moveFlag = true; moveToPos = movePos; AttackTarget.LookAt (this.transform.position); } }
// 攻击后移动的位置移动
void setMoving () { if(moveFlag == true) { transform.position = Vector3.SmoothDamp(transform.position, moveToPos, ref m_refFix, m_refcomSmoothTime); m_refcomSmoothTime -= Time.deltaTime; if(m_refcomSmoothTime <= 0) { m_refcomSmoothTime = 2.0f; moveFlag = false; this.transform.position = moveToPos; moveToPos = Vector3.zero; if(AttackTarget != null) { AttackTarget.LookAt(this.transform.position); } } if(AttackTarget != null) { this.transform.LookAt (AttackTarget.position); } } }
2. 移动的Status控制
private void FixedUpdate() { StateUpdate(); }
void StateUpdate() { if(moveFlag == true) setMoving(); if(AttackTarget != null) { if(CurrentState == CharacterAnimationStates.Attack) setMosterMoveTo(); } else { moveFlag = false; } // 根据status Animation变更 OnStateChanged(new CharacterAnimationStateChangedEventArgs(previousState, newState)); }
3. Update 中人物正常移动的AI
// 人物的正常移动AI public void Update() { float step = motionSpeed * Time.deltaTime; var motionDir = LookTarget - transform.position; if(motionDir.magnitude > Mathf.Epsilon) { var lookTowards = Quaternion.LookRotation(motionDir); transform.rotation = Quaternion.Slerp(transform.rotation, lookTowards, step); } if (CanMove) { var newPos = Vector3.MoveTowards(transform.position, Target, step); transform.position = new Vector3(newPos.x, transform.position.y, newPos.z); var d = Vector3.Distance(newPos, Target); if (d <= 1.0f && isMoving) { isMoving = false; OnReachedMoveTarget(EventArgs.Empty); } } }
标签:blog io os ar for sp 文件 div on
原文地址:http://www.cnblogs.com/liusj/p/4045473.html