欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-22 08:18:51
本文实例为大家分享了unity实现物体运动时画出轨迹的具体代码,供大家参考,具体内容如下1、新建空物体,上赋linerenderer2、新建空物体,把轨迹画出来,设计和脚本。3、linemark的脚本...

本文实例为大家分享了unity实现物体运动时画出轨迹的具体代码,供大家参考,具体内容如下

1、新建空物体,上赋linerenderer

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

2、新建空物体,把轨迹画出来,设计和脚本。

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

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、运动小球和脚本

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

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、目标小球和运动设置的脚本

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

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实现物体运动时画出轨迹

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

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