码迷,mamicode.com
首页 > 其他好文 > 详细

unity3d抛物线的脚本

时间:2014-08-17 11:40:42      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   ar   art   

 

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;
        }
    }
    
    
}

 

 

把这个脚本挂在一个cube上,然后把另一个cube拖到target上,把这两个物体之间有一段距离,然后运行,就看到效果了

unity3d抛物线的脚本,布布扣,bubuko.com

unity3d抛物线的脚本

标签:style   blog   color   os   io   for   ar   art   

原文地址:http://www.cnblogs.com/softimagewht/p/3917537.html

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