Skip to content

Instantly share code, notes, and snippets.

@HParker
Created October 12, 2019 23:53
Show Gist options
  • Save HParker/69f059566540adde23c3688b72822582 to your computer and use it in GitHub Desktop.
Save HParker/69f059566540adde23c3688b72822582 to your computer and use it in GitHub Desktop.
Simple "Can I see X" script for unity
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