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

1, 新建空物体,上赋LineRenderer

55A98B92-F226-BBBB-B0DE-5CE00851F75C.png

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

8B4046ED-A2A8-64C6-D063-0834AD128D09.png

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

F0C9DF53-3229-4C73-B77E-8AB214A13436.png

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

A6FFC560-A3CA-C9E6-8125-48319AF2772C.png

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,运行结果

6431D3B7-5008-B39F-3B29-C6A0A07B47EA.png

AEACF173-28A2-EA6F-F423-51E36BDF9631.png

收藏 (0)
评论列表
正在载入评论列表...
我是有底线的
为您推荐
    暂时没有数据