zoukankan      html  css  js  c++  java
  • unity3d抛物线的脚本

    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上,把这两个物体之间有一段距离,然后运行,就看到效果了

  • 相关阅读:
    一个主机下创建两个MySQL
    Chrome: Failed to read the 'localStorage' property from 'Window' 的解决办法
    Effective C++
    归并排序
    Daily Note
    关于Beta分布、二项分布与Dirichlet分布、多项分布的关系
    测试公式
    VLAN原理解释
    子网划分
    windows下制作debian U盘启动
  • 原文地址:https://www.cnblogs.com/softimagewht/p/3917537.html
Copyright © 2011-2022 走看看