unity3d-攻擊移動

來源:互聯網
上載者:User

標籤: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

// 人物的正常移動AIpublic 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);            }        } }

 

unity3d-攻擊移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.