using System.Collections; using System.Collections.Generic; using UnityEngine; public class BulletObject : MonoBehaviour { private bool initialied = false; [SerializeField] private Vector2 startPos; private Vector2 midPos; private float percent = 0; private float percentSpeed = 0; [SerializeField] private float speed = 25; private Vector2 lastTargetPos; // Update is called once per frame void FixedUpdate() { if (initialied ) { percent += percentSpeed * Time.deltaTime; if (percent >= 1) { percent = 1; } transform.position = UnityExpandFunction.Bezier(percent, startPos, midPos, lastTargetPos); } if(transform.position==new Vector3(lastTargetPos.x, lastTargetPos.y,0)) { Destroy(this.gameObject); } } public void Init(GameObject parent) { GameObject player = GameObject.FindGameObjectWithTag("Player"); if(player!=null) { lastTargetPos = player.transform.position; startPos = parent.transform.position+new Vector3(Random.Range(0,2), Random.Range(0, 2),0); midPos = GetMiddlePostion(parent.transform.position, lastTargetPos); percentSpeed = speed / (lastTargetPos - startPos).magnitude; transform.position = startPos; percent = 0; initialied = true; } } /// /// 中心点 /// /// /// /// private Vector2 GetMiddlePostion(Vector2 a, Vector2 b) { Vector2 m = Vector2.Lerp(a,b,0.1f); Vector2 normal = Vector2.Perpendicular(a-b).normalized; float rd = Random.Range(-2f,2f); float curveRatio = 15f;//偏移值 return m + (a - b).normalized * curveRatio * rd * normal; } private void OnTriggerEnter2D(Collider2D collision) { if(collision.CompareTag("Player")) { if (collision.gameObject.GetComponentInChildren()._animator.CheckAnimationTag("Roll")) return; Debug.Log("击中"); collision.gameObject.GetComponentInChildren().TakeDamage(20); } } }