怎么用Unity画出物体运动时的轨迹
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇内容主要讲解"怎么用Unity画出物体运动时的轨迹",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么用Unity画出物体运动时的轨迹"吧!本文实例为大
千家信息网最后更新 2026年10月31日怎么用Unity画出物体运动时的轨迹
本篇内容主要讲解"怎么用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();//获得该物体上的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();//获得该物体上的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 (); } // Update is called once per frame void Update () { transform.Translate (new Vector3(0.1f,0.1f,0.1f)); }} 6、运行结果
到此,相信大家对"怎么用Unity画出物体运动时的轨迹"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
物体
运动
轨迹
脚本
内容
目标
之间
宽度
小球
点数
组件
颜色
学习
实用
更深
代码
位置
兴趣
夹角
实例
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技深圳
鸠鸠互联网科技
以房互联网科技
嘟嘟瓜互联网科技
杭州互联网科技公司
互联网金融科技平台
上海致骋互联网科技
互联网 新科技
深圳前海聚择互联网科技有限公司
广州慧亚互联网科技怎么样
中国互联网有什么科技含量
欧洲十大科技互联网公司
成都铂石互联网科技做什么的
崇明区互联网教育科技售后服务
沐思想互联网科技有限公司
高科技互联网套路
深圳无忧互联网科技
广州互联网高科技取餐柜
互联网科技品牌
颐高云石互联网科技
盈实互联网科技
美国互联网科技
艾特科技互联网
多迪互联网科技有限公司
宁夏科技产业互联网案例
崇明区互联网教育科技售后服务
互联网5g黑科技有哪些
互联网的科技体现在哪里
互联网科技对化妆品行业
长沙易易互联网科技有限公司