カメラのスクリプトは
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class CameraControllerVer : MonoBehaviour
{
public GameObject player;
public GameObject mainCamera;
public float rotate_speed;
private const int ROTATE_BUTTON = 2;
private const float ANGLE_LIMIT_UP = 60f;
private const float ANGLE_LIMIT_DOWN = -5f;
private LockOnTargetDetector lockOnTargetDetector;
[SerializeField]
public GameObject lockOnTarget;
protected LockonCursor lockonCursor;
void Start() { mainCamera = Camera.main.gameObject; player = GameObject.FindGameObjectWithTag("Player"); lockOnTargetDetector = player.GetComponentInChildren<LockOnTargetDetector>(); lockonCursor = GameObject.FindObjectOfType<LockonCursor>(); } void LateUpdate() { transform.position = player.transform.position; rotateCmaeraNaturalAngle(); if (Input.GetMouseButton(ROTATE_BUTTON)) { rotateCmaeraAngle(); } if (Input.GetButtonDown("Fire2")) { if (lockOnTarget == null) { // ロックオンターゲット取得 GameObject target = SetTargetClosestScreenCenter(); if (target != null) { lockOnTarget = target; lockonCursor.OnLockonStart(target.transform); } else { // 視点リセット iTween.RotateTo(gameObject, iTween.Hash( "rotation", player.transform.eulerAngles, "time", 0.5f )); } } else { // 既にターゲットが設定されていた場合は解除 lockOnTarget = null; lockonCursor.OnLockonEnd(); } } if (lockOnTarget) { lockOnTargetObject(lockOnTarget); } else { rotateCmaeraNaturalAngle(); if (Input.GetMouseButton(ROTATE_BUTTON)) { rotateCmaeraAngle(); } } float angle_x = 180f <= transform.eulerAngles.x ? transform.eulerAngles.x - 360 : transform.eulerAngles.x; transform.eulerAngles = new Vector3( Mathf.Clamp(angle_x, ANGLE_LIMIT_DOWN, ANGLE_LIMIT_UP), transform.eulerAngles.y, transform.eulerAngles.z ); } private void rotateCmaeraNaturalAngle() { Vector3 angle = new Vector3( Input.GetAxis("Angles X") * rotate_speed, Input.GetAxis("Angles Y") * rotate_speed, 0 ); transform.eulerAngles += new Vector3(angle.y, angle.x); } private void rotateCmaeraAngle() { Vector3 angle = new Vector3( Input.GetAxis("Mouse X") * rotate_speed, Input.GetAxis("Mouse Y") * rotate_speed, 0 ) ; transform.eulerAngles += new Vector3(angle.y, angle.x); } private void lockOnTargetObject(GameObject target) { transform.LookAt(target.transform, Vector3.up); } protected GameObject SetTargetClosestScreenCenter() { float search_radius = 15f; var hits = Physics.SphereCastAll( player.transform.position, search_radius, player.transform.forward, 0.01f, LayerMask.NameToLayer("LockonTarget") ).Select(h => h.transform.gameObject).ToList(); hits = FilterTargetObject(hits); if (0 < hits.Count()) { float min_target_distance = float.MaxValue; GameObject target = null; foreach (var hit in hits) { Vector3 targetScreenPoint = Camera.main.WorldToViewportPoint(hit.transform.position); float target_distance = Vector2.Distance( new Vector2(0.5f, 0.5f), new Vector2(targetScreenPoint.x, targetScreenPoint.y) ); Debug.Log(hit.gameObject + ": " + target_distance); if (target_distance < min_target_distance) { min_target_distance = target_distance; target = hit.transform.gameObject; } } return target; } else { return null; } } protected List<GameObject> FilterTargetObject(List<GameObject> hits) { return hits .Where(h => { Vector3 screenPoint = Camera.main.WorldToViewportPoint(h.transform.position); return screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1; }) .Where(h => { foreach (var component in h.GetComponents<Component>()) { if (component is Enemy) { return true; } } return false; }) .ToList(); }
}
です。
ちなみに敵の位置を設定し距離でif文をかいたところNUllエラーになります。
どうすれば自動でロックオンを非表示にできるでしょうか?
回答よろしくおねがいします!
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。