unity3d抛物线的脚本
2015-10-23 12:33
435 查看
把这个脚本挂在一个cube上,然后把另一个cube拖到target上,把这两个物体之间有一段距离,然后运行,就看到效果了
using UnityEngine;
using System.Collections;
public class ProjectileTest : 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 (Shoot ());
}
IEnumerator Shoot ()
{
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);
print ("currentDist" + currentDist);
if (currentDist < 0.5f)
move = false;
this.transform.Translate (Vector3.forward * Mathf.Min (speed * Time.deltaTime, currentDist));
yield return null;
}
}
}
using UnityEngine;
using System.Collections;
public class ProjectileTest : 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 (Shoot ());
}
IEnumerator Shoot ()
{
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);
print ("currentDist" + currentDist);
if (currentDist < 0.5f)
move = false;
this.transform.Translate (Vector3.forward * Mathf.Min (speed * Time.deltaTime, currentDist));
yield return null;
}
}
}
相关文章推荐
- 【Unity3D游戏开发学习笔记】(五)灵魂降临—Unity脚本的简单运用
- Unity - 优化(二)
- 游戏开发设计模式之状态模式 & 有限状态机 & c#委托事件(unity3d 示例实现)
- 游戏开发设计模式之状态模式 & 有限状态机 & c#委托事件(unity3d 示例实现)
- 整合Unity游戏引擎和 Visual Studio Code 代码编辑器 第二弹
- UNITY 5.2.2 发行说明 中文版
- UNITY 5.2.1 发行说明 中文版
- UNITY 5.1.4 发行说明 中文版
- UNITY 4.6.9 发行说明 中文版
- List 移除时要注意的问题
- Unity3D中的系统调用方法
- Unity人工智能学习—高级随机运动
- Unity之线性渲染器
- 【NGUI】Unity中ScrollView3D滚动效果
- [原]unity5 AssetBundle打包
- Unity插件BehaviorDesigner学习资料
- 在Unity世界里遇到的第一个错误!
- unity的图像边缘检测以及简单的模糊效果
- unity5.0正式发布了,看看带来哪些重要的新特性!
- 使用VS调试Unity脚本