效果1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet_Track1 : MonoBehaviour { private Transform target; public float speed = 30; //飞行速度 public float rotationAngle = 30; //旋转角度 // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (target == null) return; Track(); } public void SetTarget(Transform target) { this.target = target; } private void Track() { Vector3 direction = target.position - transform.position; float angle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg; transform.eulerAngles = new Vector3(0, angle, 0); transform.rotation = transform.rotation * Quaternion.Euler(0, rotationAngle, 0); transform.Translate(Vector3.forward * speed * Time.deltaTime); if (Vector3.Distance(transform.position, target.position) <= 0.5f) { Destroy(gameObject); } } } |
效果2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet_Track2 : MonoBehaviour { private Transform target; public float speed = 30; //飞行速度 public float startAngle = 30; //初始角度 public float radian = 1; //弧度 // Start is called before the first frame update void Start() { transform.eulerAngles = new Vector3(0, startAngle, 0); } // Update is called once per frame void Update() { if (target == null) return; Track(); } public void SetTarget(Transform target) { this.target = target; } private void Track() { transform.forward = Vector3.Slerp(transform.forward, target.position - transform.position, radian / Vector3.Distance(transform.position, target.position)); transform.position += transform.forward * speed * Time.deltaTime; if (Vector3.Distance(transform.position, target.position) <= 0.5f) { Destroy(gameObject); } } } |
GitHub下载地址:
https://github.com/654306663/BulletTrajectory.git
- 本文固定链接: http://www.u3d8.com/?p=2054
- 转载请注明: 网虫虫 在 u3d8.com 发表过
很棒 感谢作者大佬