Unity實(shí)現(xiàn)物體運(yùn)動(dòng)時(shí)畫出軌跡
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)物體運(yùn)動(dòng)時(shí)畫出軌跡的具體代碼,供大家參考,具體內(nèi)容如下
1、新建空物體,上賦LineRenderer

2、新建空物體,把軌跡畫出來,設(shè)計(jì)和腳本。

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);//克隆一個(gè)帶有LineRender的物體
line = clone.GetComponent<LineRenderer>();//獲得該物體上的LineRender組件
// //line.SetColors(Color.blue, Color.red);//設(shè)置顏色
// //line.SetWidth(0.2f, 0.1f);//設(shè)置寬度
i = 0;
}
// Update is called once per frame
void Update () {
RunNext = run.transform.position;
if (RunStart != RunNext) {
i++;
line.SetVertexCount(i);//設(shè)置頂點(diǎn)數(shù)
line.SetPosition(i-1, run.transform.position);
}
RunStart = RunNext;
// if (Input.GetMouseButtonDown(0))
// {
// clone = (GameObject)Instantiate(obs, obs.transform.position, transform.rotation);//克隆一個(gè)帶有LineRender的物體
// line = clone.GetComponent<LineRenderer>();//獲得該物體上的LineRender組件
// line.SetColors(Color.blue, Color.red);//設(shè)置顏色
// line.SetWidth(0.2f, 0.1f);//設(shè)置寬度
// i = 0;
// print ("GetMouseButtonDown");
// }
// if (Input.GetMouseButton(0))
// {
// i++;
// line.SetVertexCount(i);//設(shè)置頂點(diǎn)數(shù)
// line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));//設(shè)置頂點(diǎn)位置
// print ("GetMouseButton");
//
// }
}
}
4、運(yùn)動(dòng)小球和腳本
Run.cs
using UnityEngine;
using System.Collections;
public class Run : MonoBehaviour
{
public GameObject target; //要到達(dá)的目標(biāo)
public float speed = 10; //速度
private float distanceToTarget; //兩者之間的距離
private bool move = true;
void Start()
{
//計(jì)算兩者之間的距離
distanceToTarget = Vector3.Distance(this.transform.position, target.transform.position);
StartCoroutine(StartShoot());
}
IEnumerator StartShoot()
{
while (move)
{
Vector3 targetPos = target.transform.position;
//讓始終它朝著目標(biāo)
this.transform.LookAt(targetPos);
//計(jì)算弧線中的夾角
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、目標(biāo)小球和運(yùn)動(dòng)設(shè)置的腳本

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、運(yùn)行結(jié)果


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# WebService發(fā)布以及IIS發(fā)布
這篇文章主要介紹了C# WebService發(fā)布以及IIS發(fā)布的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
C#獲取文件、文件夾和驅(qū)動(dòng)器的信息示例詳解
在C#中,文件、文件夾和驅(qū)動(dòng)器是文件系統(tǒng)操作的基本元素,了解如何獲取這些元素的信息對(duì)于開發(fā)文件處理和管理工具至關(guān)重要,本文將詳細(xì)介紹如何在C#中獲取有關(guān)文件、文件夾和驅(qū)動(dòng)器的信息,并提供相應(yīng)的示例,需要的朋友可以參考下2024-08-08
C#實(shí)現(xiàn)將字符串轉(zhuǎn)化為日期格式的方法詳解
這篇文章主要為大家詳細(xì)介紹了C#如何使用DateTime結(jié)構(gòu)的ParseExact方法和Parse方法分別將字符串轉(zhuǎn)化為日期格式,有需要的小伙伴可以了解一下2024-01-01
關(guān)于C#基礎(chǔ)知識(shí)回顧--反射(二)
其實(shí)說白了,反射就是能知道我們未知類型的類型信息這么一個(gè)東西.沒什么神秘可講!反射的核心是System.Type。System.Type包含了很多屬性和方法,使用這些屬性和方法可以在運(yùn)行時(shí)得到類型信息2013-07-07
C#修改及重置電腦密碼DirectoryEntry實(shí)現(xiàn)方法
這篇文章主要介紹了C#修改及重置電腦密碼DirectoryEntry實(shí)現(xiàn)方法,實(shí)例分析了C#修改及重置電腦密碼的相關(guān)技巧,需要的朋友可以參考下2015-05-05
C#實(shí)現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法
這篇文章主要介紹了C#實(shí)現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
C#實(shí)現(xiàn)SMTP郵件附件發(fā)送功能詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)SMTP郵件附件發(fā)送的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12

