照準器を作成する



照準器の実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LockOn : MonoBehaviour
{
public float lockRange = 50f;
public Image aimImage;
private Camera FPSCamera;
private Color originalColor;
void Start()
{
FPSCamera = GetComponent<Camera>();
originalColor = aimImage.color;
}
void Update()
{
Vector3 rayOrigin = FPSCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if(Physics.Raycast(rayOrigin, FPSCamera.transform.forward, out hit, lockRange))
{
string hitTag = hit.transform.gameObject.tag;
if(hitTag == "Target")
{
// 赤色に変更
aimImage.color = new Color(1.0f, 0f, 0f, 1.0f);
}
else
{
aimImage.color = originalColor;
}
}
}
}



【2019版】X_Mission(基礎/全51回)
他のコースを見る


照準器の実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LockOn : MonoBehaviour
{
public float lockRange = 50f;
public Image aimImage;
private Camera FPSCamera;
private Color originalColor;
void Start()
{
FPSCamera = GetComponent<Camera>();
originalColor = aimImage.color;
}
void Update()
{
Vector3 rayOrigin = FPSCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if(Physics.Raycast(rayOrigin, FPSCamera.transform.forward, out hit, lockRange))
{
string hitTag = hit.transform.gameObject.tag;
if(hitTag == "Target")
{
// 赤色に変更
aimImage.color = new Color(1.0f, 0f, 0f, 1.0f);
}
else
{
aimImage.color = originalColor;
}
}
}
}



照準器を作成する