歡迎您光臨本站 註冊首頁

unity實現弧形移動 可角度自定

←手機掃碼閱讀     techdo @ 2020-06-22 , reply:0

本文實例為大家分享了unity實現弧形移動的具體代碼,自定角度,供大家參考,具體內容如下

兩點之間弧形移動

  using UnityEngine;  using System.Collections;     public class MoveTest : MonoBehaviour  {    public GameObject target;  //要到達的目標     public float speed = 10;  //速度     public int rotationAngle = 60;    private float distanceToTarget;  //兩者之間的距離     private bool move = true;       void Start()    {      //計算兩者之間的距離       distanceToTarget = Vector3.Distance(this.transform.position, target.transform.position);      StartCoroutine(Move());    }       IEnumerator Move()    {         while (move) //移動到目標點停止移動      {        Vector3 targetPos = target.transform.position;           //讓始終它朝著目標         this.transform.LookAt(targetPos);           //計算弧線中的夾角         float angle = Mathf.Min(1, Vector3.Distance(this.transform.position, targetPos) / distanceToTarget) * rotationAngle;        this.transform.rotation = this.transform.rotation * Quaternion.Euler(Mathf.Clamp(-angle, -42, 42), 0, 0);        float currentDist = Vector3.Distance(this.transform.position, target.transform.position);        if (currentDist < 0.5f)          move = false;        this.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist));        yield return null;      }    }        }

 

                                                       

   


[techdo ] unity實現弧形移動 可角度自定已經有265次圍觀

http://coctec.com/docs/program/show-post-239417.html