效果如圖 使物體Y軸始終朝向目標物體 用于實現2D版子彈跟蹤功能
實現方法:
public Transform _collider_2; void Update() { transform.eulerAngles = LookTargetAngle(transform.position,_collider_2.position); } Vector3 LookTargetAngle(Vector3 playerPos, Vector3 targetPos) { float dx = targetPos.x - playerPos.x; float dy = targetPos.y - playerPos.y; float rotationZ = Mathf.Atan2(dy, dx) * 180 / Mathf.PI; //得到最終的角度并且確保在 [0, 360) 這個區間內 rotationZ -= 90; //獲取增加的角度 float originRotationZ = this.transform.eulerAngles.z; float addRotationZ = rotationZ - originRotationZ; //超過 180 度需要修改為負方向的角度 if (addRotationZ > 180) { addRotationZ -= 360; } //應用旋轉 return new Vector3(0, 0, this.transform.eulerAngles.z + addRotationZ); }借鑒自: http://www.bubuko.com/infodetail-1243233.html
新聞熱點
疑難解答