Unity实现物体运动时画出轨迹

 更新时间:2021年09月12日 17:18:20   作者:天人合一peng  
这篇文章主要为大家详细介绍了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、运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C#中的Task.WaitAll和Task.WaitAny方法介绍

    C#中的Task.WaitAll和Task.WaitAny方法介绍

    这篇文章介绍了C#中的Task.WaitAll和Task.WaitAny方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#部署数据库及IIS站点

    C#部署数据库及IIS站点

    这篇文章主要为大家详细介绍了C#部署数据库及IIS站点的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • C#取得随机颜色的方法

    C#取得随机颜色的方法

    这篇文章主要介绍了C#取得随机颜色的方法,通过自定义函数实现随机颜色的功能,非常具有实用价值,需要的朋友可以参考下
    2015-01-01
  • C#获取Word文档中所有表格的实现代码分享

    C#获取Word文档中所有表格的实现代码分享

    这篇文章主要介绍了C#获取Word文档中所有表格的实现代码分享,小编亲测可用,需要的朋友可以参考下
    2014-09-09
  • C#/.Net 中快速批量给SQLite数据库插入测试数据

    C#/.Net 中快速批量给SQLite数据库插入测试数据

    这篇文章主要介绍了C#/.Net 中快速批量给SQLite数据库插入测试数据,本文直接给出实例代码,需要的朋友可以参考下
    2015-06-06
  • 一文详解如何在C#中使用RESTful API

    一文详解如何在C#中使用RESTful API

    通过Web开发的路径,大家会发现自己迟早需要处理外部API,本文将列出在C#项目中使用RESTful API的方法的最全面列表,希望对大家有所帮助
    2025-03-03
  • C#不可变类型深入解析

    C#不可变类型深入解析

    这篇文章主要介绍了C#不可变类型,对于C#程序员深入理解string数据类型有很大的帮助,需要的朋友可以参考下
    2014-08-08
  • C# SendMail发送邮件功能实现

    C# SendMail发送邮件功能实现

    这篇文章主要为大家详细介绍了C# SendMail发送邮件功能实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#中如何把dll打包到exe

    C#中如何把dll打包到exe

    这篇文章主要介绍了C#中如何把dll打包到exe问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 在.NET中读取嵌入和使用资源文件的方法

    在.NET中读取嵌入和使用资源文件的方法

    本文分别介绍了使用GetManifestResourceStream读取嵌入资源,和使用. resx资源文件嵌入资源,希望对大家有所帮助。
    2016-05-05

最新评论