您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么用Unity畫出物體運動時的軌跡”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用Unity畫出物體運動時的軌跡”吧!
本文實例為大家分享了Unity實現物體運動時畫出軌跡的具體代碼,供大家參考,具體內容如下
1、新建空物體,上賦LineRenderer
2、新建空物體,把軌跡畫出來,設計和腳本。
3、LineMark的腳本是
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LineMark : MonoBehaviour { private GameObject clone; private LineRenderer line; private int i; public GameObject obs; public GameObject run; Vector3 RunStart; Vector3 RunNext; // Use this for initialization void Start () { RunStart = run.transform.position; clone = (GameObject)Instantiate(obs, run.transform.position, run.transform.rotation);//克隆一個帶有LineRender的物體 line = clone.GetComponent<LineRenderer>();//獲得該物體上的LineRender組件 // //line.SetColors(Color.blue, Color.red);//設置顏色 // //line.SetWidth(0.2f, 0.1f);//設置寬度 i = 0; } // Update is called once per frame void Update () { RunNext = run.transform.position; if (RunStart != RunNext) { i++; line.SetVertexCount(i);//設置頂點數 line.SetPosition(i-1, run.transform.position); } RunStart = RunNext; // if (Input.GetMouseButtonDown(0)) // { // clone = (GameObject)Instantiate(obs, obs.transform.position, transform.rotation);//克隆一個帶有LineRender的物體 // line = clone.GetComponent<LineRenderer>();//獲得該物體上的LineRender組件 // line.SetColors(Color.blue, Color.red);//設置顏色 // line.SetWidth(0.2f, 0.1f);//設置寬度 // i = 0; // print ("GetMouseButtonDown"); // } // if (Input.GetMouseButton(0)) // { // i++; // line.SetVertexCount(i);//設置頂點數 // line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));//設置頂點位置 // print ("GetMouseButton"); // // } } }
4、運動小球和腳本
Run.cs
using UnityEngine; using System.Collections; public class Run : MonoBehaviour { public GameObject target; //要到達的目標 public float speed = 10; //速度 private float distanceToTarget; //兩者之間的距離 private bool move = true; void Start() { //計算兩者之間的距離 distanceToTarget = Vector3.Distance(this.transform.position, target.transform.position); StartCoroutine(StartShoot()); } IEnumerator StartShoot() { while (move) { Vector3 targetPos = target.transform.position; //讓始終它朝著目標 this.transform.LookAt(targetPos); //計算弧線中的夾角 float angle = Mathf.Min(1, Vector3.Distance(this.transform.position, targetPos) / distanceToTarget) * 45; 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 = true; this.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist)); yield return null; } } }
5、目標小球和運動設置的腳本
follew.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class followme : MonoBehaviour { Rigidbody follew; // Use this for initialization void Start () { follew = GetComponent<Rigidbody> (); } // Update is called once per frame void Update () { transform.Translate (new Vector3(0.1f,0.1f,0.1f)); } }
6、運行結果
到此,相信大家對“怎么用Unity畫出物體運動時的軌跡”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。