Created
October 12, 2019 23:53
-
-
Save HParker/69f059566540adde23c3688b72822582 to your computer and use it in GitHub Desktop.
Simple "Can I see X" script for unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class looker : MonoBehaviour | |
{ | |
[SerializeField] private GameObject target; | |
[SerializeField] private float viewAngle = 30f; | |
[SerializeField] public RaycastHit hit; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
Vector3 targetDir = target.transform.position - transform.position; | |
float angle = Vector3.Angle(targetDir, transform.forward); | |
if (Physics.Raycast( | |
transform.position, | |
transform.TransformDirection(targetDir), | |
out hit, | |
15f)) | |
{ | |
print(hit.collider.gameObject.name); | |
if (angle < viewAngle && target.name == hit.collider.gameObject.name) | |
{ | |
Debug.DrawRay(transform.position, transform.TransformDirection(targetDir) * hit.distance, Color.red); | |
} else | |
{ | |
// Debug.DrawRay(transform.position, transform.TransformDirection(targetDir) * hit.distance, Color.yellow); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment