using System.Collections; using System.Collections.Generic; using UnityEngine; public class ControlCamera : MonoBehaviour { //ballオブジェクト [SerializeField] GameObject ball; [SerializeField] GameObject[] Goal = new GameObject[2]; //ボールを持っているキャラクターのオブジェクト public GameObject charBall = null; public int cameraType = 0; //初期設定 void Start() { //オブジェクトを取得 ball = GameObject.Find("ball").gameObject; Goal[0] = GameObject.Find("Goal Post0").gameObject; Goal[1] = GameObject.Find("Goal Post1").gameObject; } void Update() { //---------------------------------------------------------- //ターゲットが動いたときにカメラを追従 if (charBall == null) //ballオブジェクト //---------------------------------------------------------- { FollowCamera(ball); } else if (charBall != null) //ボールを持つキャラクター { FollowCamera(charBall); } } //カメラの追従 void FollowCamera(GameObject target) { Vector3 targetpos = target.transform.position; //対象の座標 Vector3 mypos = transform.position; //自分の座標 if (cameraType == 0) { float initz = 70f; //対象から後ろに離れる距離 float timesp = 2f; //追従の速さ float dis = 1f; //追従をしなくていい距離 if (target.tag == "ball") { ///=================================================================================================================================================== //カメラの向き cameraAngleType0(mypos); ///=================================================================================================================================================== } // キャラクターの場合 else if (target.tag == "Player") { if (target.GetComponent().teamNum == 1) { initz = 100f; //Debug.Log("敵チームだ"); } ///=================================================================================================================================================== //カメラの向き cameraAngleType0(mypos); ///=================================================================================================================================================== } ///=================================================================================================================================================== //カメラの追尾 cameraMoveType0(mypos, targetpos, timesp, dis, initz); ///=================================================================================================================================================== } //シュート時の制御 if (cameraType == 1) { //ボールの場合 if (target.tag == "ball") { float zoomout = 40f; //対象から後ろに離れる距離 float timesp = 2f; //追従の速さ float dis = 0.1f; //追従をしなくていい距離 //ボールに追従するため、ゴールからボールへの単位ベクトル Vector3 normal = Vector3.zero; //カメラの移動先の座標 Vector3 movepos = Vector3.zero; if (GameObject.Find("Manager").GetComponent().teamBall[0]) { normal = (targetpos - Goal[0].transform.FindChild("GoalTargetPoint").transform.position).normalized; } else if(GameObject.Find("Manager").GetComponent().teamBall[1]) { normal = (targetpos - Goal[1].transform.FindChild("GoalTargetPoint").transform.position).normalized; } //もとめた単位ベクトルにzoomoutをかけて、ボールに追従する座標をもとめる if (normal != Vector3.zero) { movepos = new Vector3(targetpos.x + normal.x * zoomout, targetpos.y + normal.y * zoomout, targetpos.z + normal.z * zoomout); } ///=================================================================================================================================================== //カメラの向き cameraAngleType1(mypos, targetpos); ///=================================================================================================================================================== ///=================================================================================================================================================== //カメラの追尾 cameraMoveType1(mypos, new Vector3(movepos.x, targetpos.y, movepos.z), timesp, dis); ///=================================================================================================================================================== } if (target.tag == "Player") { float zoomout = 10f; //対象から後ろに離れる距離 float timesp = 10f; //追従の速さ float dis = 1f; //追従をしなくていい距離 //キャラクターに追従するため、ゴールからキャラクターへの単位ベクトル Vector3 normal = (target.transform.position - Goal[target.GetComponent().teamNum].transform.FindChild("GoalTargetPoint").transform.position).normalized; //もとめた単位ベクトルにzoomoutをかけて、キャラクターに追従する座標をもとめる Vector3 movepos = new Vector3(targetpos.x + normal.x * zoomout, targetpos.y + normal.y * zoomout, targetpos.z + normal.z * zoomout); ///=================================================================================================================================================== //カメラの向き cameraAngleType1(mypos, Goal[target.GetComponent().teamNum].transform.FindChild("GoalTargetPoint").transform.position); ///=================================================================================================================================================== ///=================================================================================================================================================== //カメラの追尾 cameraMoveType1(mypos, new Vector3(movepos.x, 8f, movepos.z), timesp, dis); ///=================================================================================================================================================== } } // if (cameraType == 2) { } } //タイプ0のカメラアングル (自分の座標) void cameraAngleType0(Vector3 mypos) { //角度の取得 カメラからカメラを向けたい物へのベクトルを取得する var diff = (Goal[0].transform.FindChild("GoalTargetPoint").transform.position - mypos).normalized; Quaternion tmprot = Quaternion.FromToRotation(Vector3.forward, diff); float eulerAngleY = tmprot.eulerAngles.y; if (eulerAngleY > 180f) { eulerAngleY -= 360f; } float angleY = eulerAngleY; //非数値の場合は代入しない if (!float.IsNaN(angleY)) { transform.rotation = Quaternion.Euler(17f, angleY * 0.5f, 0f); } } //タイプ0のカメラ移動 (カメラの座標、対象の座標、移動の速さ、追従をしなくていい距離、対象から後ろに離れる距離) void cameraMoveType0(Vector3 mypos, Vector3 targetpos, float timesp, float dis, float initz) { float tx = mypos.x; float ty = mypos.y; float tz = mypos.z; //x座標 if (Mathf.Abs(targetpos.x - mypos.x) > dis) { tx = mypos.x + (targetpos.x - mypos.x) * timesp * Time.deltaTime; //非数値の場合は代入しない if (!float.IsNaN(tx)) { transform.position = new Vector3(tx, transform.position.y, transform.position.z); } } //y座標 y座標だけ30fになるようにする if (Mathf.Abs(30f - mypos.y) > 0.1f) { ty = mypos.y + (30f - mypos.y) * timesp * Time.deltaTime; //非数値の場合は代入しない if (!float.IsNaN(ty)) { transform.position = new Vector3(transform.position.x, ty, transform.position.z); } } else if (mypos.y != 30f) { transform.position = new Vector3(transform.position.x, 30f, transform.position.z); } //z座標 if (Mathf.Abs(targetpos.z - initz - transform.position.z) > dis) { tz = mypos.z + (targetpos.z - initz - mypos.z) * timesp * Time.deltaTime; //非数値の場合は代入しない if (!float.IsNaN(tz)) { transform.position = new Vector3(transform.position.x, transform.position.y, tz); } } } //タイプ1のカメラアングル (自分の座標、対象の座標) void cameraAngleType1(Vector3 mypos, Vector3 targetpos) { //角度の取得 カメラからカメラを向けたい物へのベクトルを取得する var diff = (targetpos - mypos).normalized; Quaternion tmprot = Quaternion.FromToRotation(Vector3.forward, diff); float eulerAngleX = tmprot.eulerAngles.x; float eulerAngleY = tmprot.eulerAngles.y; if (eulerAngleY > 180f) { eulerAngleY -= 360f; } float angleX = eulerAngleX; float angleY = eulerAngleY; if (!float.IsNaN(angleX)) { transform.rotation = Quaternion.Euler(angleX, transform.eulerAngles.y, 0f); } if (!float.IsNaN(angleY)) { transform.rotation = Quaternion.Euler(transform.eulerAngles.x, angleY, 0f); } } //タイプ1のカメラ移動 (カメラの座標、移動先の座標、移動の速さ、追従をしなくていい距離) void cameraMoveType1(Vector3 mypos, Vector3 movepos, float timesp, float dis) { float tx = mypos.x; float ty = mypos.y; float tz = mypos.z; //x座標 if (Mathf.Abs(movepos.x - mypos.x) > dis) { tx = mypos.x + (movepos.x - mypos.x) * timesp * Time.deltaTime; if (!float.IsNaN(tx)) { transform.position = new Vector3(tx, transform.position.y, transform.position.z); } } //y座標 if (Mathf.Abs(movepos.y - mypos.y) > dis) { ty = mypos.y + (movepos.y - mypos.y) * timesp * Time.deltaTime; if (!float.IsNaN(ty)) { transform.position = new Vector3(transform.position.x, ty, transform.position.z); } } //z座標 if (Mathf.Abs(movepos.z - mypos.z) > dis) { tz = mypos.z + (movepos.z - mypos.z) * timesp * Time.deltaTime; if (!float.IsNaN(tz)) { transform.position = new Vector3(transform.position.x, transform.position.y, tz); } } } }