一人称視点専用の照準器を作る





照準器の実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 追加
using UnityEngine.UI;
public class AimController : MonoBehaviour
{
public Image aimImage;
public GameObject aim;
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.transform.CompareTag("Enemy"))
{
// 照準器を赤色に変化させる。
aimImage.color = Color.red;
// aimの大きさを小さくする
aim.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
}
else
{
// 照準器を白に戻す。
aimImage.color = Color.white;
// aimの大きさを元に戻す
aim.transform.localScale = new Vector3(1, 1, 1);
}
}
else
{
aimImage.color = Color.white;
aim.transform.localScale = new Vector3(1, 1, 1);
}
}
}










照準器の実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 追加
using UnityEngine.UI;
public class AimController : MonoBehaviour
{
public Image aimImage;
public GameObject aim;
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.transform.CompareTag("Enemy"))
{
// 照準器を赤色に変化させる。
aimImage.color = Color.red;
// aimの大きさを小さくする
aim.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
}
else
{
// 照準器を白に戻す。
aimImage.color = Color.white;
// aimの大きさを元に戻す
aim.transform.localScale = new Vector3(1, 1, 1);
}
}
else
{
aimImage.color = Color.white;
aim.transform.localScale = new Vector3(1, 1, 1);
}
}
}





一人称視点専用の照準器を作る