炮弹目标追踪效果

2016-3-18 雨辰 Unity3D

using UnityEngine;
using System.Collections;

/// <summary>
/// 追踪效果
/// </summary>
public class ParticleMoveItem : MonoBehaviour 
{
	private float speed = 15f;
	private bool moveStatus = false;

	private GameObject targetObject;
	private Vector3 direction;

	public void ChangeData(GameObject targetObject)
	{
		this.moveStatus = true;
		this.targetObject = targetObject;
	}

	void Update()
	{
		if(!this.moveStatus) return;

		this.direction = (this.targetObject.transform.position - this.transform.position).normalized * this.speed;
		// 看向目标
		this.transform.LookAt (this.targetObject.transform.position);

		if(!IsArrivePosition())
		{
			this.transform.position += direction * Time.deltaTime;
		}else{
			this.moveStatus = false;
			// 设置目标点
			this.transform.position = this.targetObject.transform.position;
			// 销毁对象
			Destroy(this.gameObject);
		}
	}
	
	private bool IsArrivePosition()
	{
		Vector3 currentDirection = (this.targetObject.transform.position - (this.transform.position + this.direction * Time.deltaTime)).normalized;
		if (this.CalculateNormalized (currentDirection) == this.CalculateNormalized (this.direction) * -1) 
		{
			return true;
		}
		return false;
	}
	
	private Vector3 CalculateNormalized(Vector3 data)
	{
		Vector3 position = Vector3.zero;
		if(data.x != 0) position.x = data.x > 0 ? 1 : -1;
		if(data.z != 0) position.z = data.z > 0 ? 1 : -1;
		return position;
	}
}

标签: Unity3D-优化性能 Unity3D

发表评论:

雨辰 joyimp|@2011-2018 京ICP备16030765号