Unity炮弹拖尾和弓箭的抛物运动方法
2016-01-19 16:53
363 查看
最近遇到了一个很有趣的问题,就是关于炮弹(其实炮弹加上拖尾和弓箭是一样的,这里统一用炮弹说了)发射的问题,炮弹的发射很简单,但是关键在于如果动态的改变炮弹拖尾的方向,额,废话不多说了:
一、给出炮弹的目标点,和运行时的速度。(处理了拖尾效果)
public GameObject target;
public float moveSpeed = 10;
private float distance;
private bool isArived = false;
void Start()
{
distance = Vector3.Distance(this.transform.position, target.transform.position);
StartCoroutine(Shoot());
}
IEnumerator Shoot()
{
while (isArived == false)
{
Vector3 targetPosition = target.transform.position;
this.transform.LookAt(targetPosition);
float angle = Mathf.Min(1,Vector3.Distance(this.transform.position,targetPosition)/distance) *45; //弧度夹角
this.transform.rotation = this.transform.rotation *Quaternion.Euler(Mathf.Clamp(-angle,-45,45),0,0);
float currentDis = Vector3.Distance(this.transform.position,target.transform.position);
this.transform.Translate(Vector3.forward *Mathf.Min(moveSpeed *Time.deltaTime,currentDis));
if (currentDis < 0.2f)
{
isArived = true;
}
yield return null;
}
}
一、给出炮弹的目标点,和运行时的速度。(处理了拖尾效果)
public GameObject target;
public float moveSpeed = 10;
private float distance;
private bool isArived = false;
void Start()
{
distance = Vector3.Distance(this.transform.position, target.transform.position);
StartCoroutine(Shoot());
}
IEnumerator Shoot()
{
while (isArived == false)
{
Vector3 targetPosition = target.transform.position;
this.transform.LookAt(targetPosition);
float angle = Mathf.Min(1,Vector3.Distance(this.transform.position,targetPosition)/distance) *45; //弧度夹角
this.transform.rotation = this.transform.rotation *Quaternion.Euler(Mathf.Clamp(-angle,-45,45),0,0);
float currentDis = Vector3.Distance(this.transform.position,target.transform.position);
this.transform.Translate(Vector3.forward *Mathf.Min(moveSpeed *Time.deltaTime,currentDis));
if (currentDis < 0.2f)
{
isArived = true;
}
yield return null;
}
}
相关文章推荐
- Unity3D的四种坐标系
- Unity 3D中的 SetActive() 与 OnEnable()、OnDisable()要注意的
- unity中刷路点
- 【小松教你手游开发】【unity实用技能】Unity3D_NGUI_性能优化实践_CPU卡顿
- 如何让Maya中的相机和Unity相机的FOV(Angle of View)对上
- 【小松教你手游开发】【unity实用技能】Unity3D中Assetbundle技术使用心得
- Unity3d 开发(七)AssetBundle组织目录
- Unity学习日记-使用脚本进行音频资源的读取,Resources.LoadAll
- unity, GUI.Button texture is black
- unity3d 简单的xml文件读写操作
- unity5.0新功能-布料、动画系统
- Unity C#多态
- Unity3d 综合性能窍门
- 初学unity3D-2016/01/18
- UNITY学习的第一天总结
- UNITY 5.2.3 & UNITY 5.2.4 发行说明 中文版
- Unity课程第一节
- unity AR开发中遇到的一些错误总结
- unity 3D 学习
- unity学习(一)