码迷,mamicode.com
首页 > 编程语言 > 详细

Unity 弹道轨迹

时间:2015-04-05 15:52:39      阅读:727      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 

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);
            this.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist));
            
            //如果距离目标0.5就停止移动
            if (currentDist < 0.5f) 
            {
                move = false;
            }
            

            yield return null;
        }
    }


}

Unity 弹道轨迹

标签:

原文地址:http://www.cnblogs.com/plateFace/p/4394095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!