照準器を作る







Rayで照準器を作る
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 追加しましょう(ポイント)
using UnityEngine.UI;
public class Aim : MonoBehaviour
{
[SerializeField]
private Image aimImage;
void Update()
{
// レーザー(ray)を飛ばす「起点」と「方向」
Ray ray = new Ray(transform.position, transform.forward);
// rayのあたり判定の情報を入れる箱を作る。
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 60))
{
string hitName = hit.transform.gameObject.tag;
if (hitName == "Enemy")
{
// 照準器の色を「赤」に変える(色は自由に変更してください。)
aimImage.color = new Color(1.0f, 0.0f, 0.0f, 1.0f);
}
else
{
// 照準器の色を「水色」(色は自由に変更してください。)
aimImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f);
}
}
else
{
// 照準器の色を「水色」(色は自由に変更してください。)
aimImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f);
}
}
}











Rayを可視化する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Aim : MonoBehaviour
{
[SerializeField]
private Image aimImage;
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);
// ★★追加(レーザー光を可視化することができる)
Debug.DrawRay(transform.position, transform.forward * 60, Color.green);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 60))
{
string hitName = hit.transform.gameObject.tag;
if (hitName == "Enemy")
{
aimImage.color = new Color(1.0f, 0.0f, 0.0f, 1.0f);
}
else
{
aimImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f);
}
}
else
{
aimImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f);
}
}
}




照準器のオンオフを切り替える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
[SerializeField]
private Camera mainCamera;
[SerializeField]
private Camera FPSCamera;
private bool mainCameraON = true;
[SerializeField]
private AudioListener mainListener;
[SerializeField]
private AudioListener FPSListener;
// ★追加
[SerializeField]
private GameObject aimImage;
void Start()
{
mainCamera.enabled = true;
FPSCamera.enabled = false;
mainListener.enabled = true;
FPSListener.enabled = false; // FPSカメラはオフ
// ★追加
// (発想)主観カメラ(FPSカメラ)がオンの時だけ、照準器もオンにする。
aimImage.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C) && mainCameraON == true)
{
mainCamera.enabled = false;
FPSCamera.enabled = true;
mainCameraON = false;
mainListener.enabled = false;
FPSListener.enabled = true; // FPSカメラはオン
// ★追加
aimImage.SetActive(true);
}
else if (Input.GetKeyDown(KeyCode.C) && mainCameraON == false)
{
mainCamera.enabled = true;
FPSCamera.enabled = false;
mainCameraON = true;
mainListener.enabled = true;
FPSListener.enabled = false; // FPSカメラはオフ
// ★追加
aimImage.SetActive(false);
}
}
}



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






Rayで照準器を作る
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 追加しましょう(ポイント)
using UnityEngine.UI;
public class Aim : MonoBehaviour
{
[SerializeField]
private Image aimImage;
void Update()
{
// レーザー(ray)を飛ばす「起点」と「方向」
Ray ray = new Ray(transform.position, transform.forward);
// rayのあたり判定の情報を入れる箱を作る。
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 60))
{
string hitName = hit.transform.gameObject.tag;
if (hitName == "Enemy")
{
// 照準器の色を「赤」に変える(色は自由に変更してください。)
aimImage.color = new Color(1.0f, 0.0f, 0.0f, 1.0f);
}
else
{
// 照準器の色を「水色」(色は自由に変更してください。)
aimImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f);
}
}
else
{
// 照準器の色を「水色」(色は自由に変更してください。)
aimImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f);
}
}
}











Rayを可視化する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Aim : MonoBehaviour
{
[SerializeField]
private Image aimImage;
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);
// ★★追加(レーザー光を可視化することができる)
Debug.DrawRay(transform.position, transform.forward * 60, Color.green);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 60))
{
string hitName = hit.transform.gameObject.tag;
if (hitName == "Enemy")
{
aimImage.color = new Color(1.0f, 0.0f, 0.0f, 1.0f);
}
else
{
aimImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f);
}
}
else
{
aimImage.color = new Color(0.0f, 1.0f, 1.0f, 1.0f);
}
}
}




照準器のオンオフを切り替える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
[SerializeField]
private Camera mainCamera;
[SerializeField]
private Camera FPSCamera;
private bool mainCameraON = true;
[SerializeField]
private AudioListener mainListener;
[SerializeField]
private AudioListener FPSListener;
// ★追加
[SerializeField]
private GameObject aimImage;
void Start()
{
mainCamera.enabled = true;
FPSCamera.enabled = false;
mainListener.enabled = true;
FPSListener.enabled = false; // FPSカメラはオフ
// ★追加
// (発想)主観カメラ(FPSカメラ)がオンの時だけ、照準器もオンにする。
aimImage.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C) && mainCameraON == true)
{
mainCamera.enabled = false;
FPSCamera.enabled = true;
mainCameraON = false;
mainListener.enabled = false;
FPSListener.enabled = true; // FPSカメラはオン
// ★追加
aimImage.SetActive(true);
}
else if (Input.GetKeyDown(KeyCode.C) && mainCameraON == false)
{
mainCamera.enabled = true;
FPSCamera.enabled = false;
mainCameraON = true;
mainListener.enabled = true;
FPSListener.enabled = false; // FPSカメラはオフ
// ★追加
aimImage.SetActive(false);
}
}
}



照準器を作る