标签:
1 using UnityEngine; 2 3 using System.Collections; 4 5 public class ProjectileTest : MonoBehaviour 6 { 7 8 public GameObject target; 9 10 public float speed = 10; 11 12 private float distanceToTarget; 13 14 private bool move = true; 15 16 void Start() 17 { 18 19 distanceToTarget = Vector3.Distance(this.transform.position, target.transform.position); 20 21 StartCoroutine(Shoot()); 22 23 } 24 25 IEnumerator Shoot() 26 { 27 28 while (move) 29 { 30 31 Vector3 targetPos = target.transform.position; 32 33 this.transform.LookAt(targetPos); 34 35 float angle = Mathf.Min(1, Vector3.Distance(this.transform.position, targetPos) / distanceToTarget) * 45; 36 37 this.transform.rotation = this.transform.rotation * Quaternion.Euler(Mathf.Clamp(-angle, -42, 42), 0, 0); 38 39 float currentDist = Vector3.Distance(this.transform.position, target.transform.position); 40 41 print("“currentDist”" + currentDist); 42 43 if (currentDist < 0.5f) 44 45 move = false; 46 47 this.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist)); 48 49 yield return null; 50 51 } 52 53 } 54 55 }
标签:
原文地址:http://www.cnblogs.com/ssw-men/p/4272149.html