I have a security camera and a collider attached to the detection area. I want the script to detect that the colliding object is a player, and then using a raycast to determine if the player is behind one of the two ledges they can hide behind.
[17781-unity+assignmeny.png|17781]
How can I do this? I believe I would have to find the position of the camera and for the origin point, but how can I determine the direction (the camera moves side to side 45 degrees) and length to send the ray?
My script is currently:
import UnityEngine
class detection_script (MonoBehaviour):
def Start ():
pass
def FixedUpdate ():
pass
def OnTriggerStay(collisionInfo as Collider):
if collisionInfo.collider.tag == "Player":
Attach a script to the camera itself and use the direction of the camera to determine where the Ray is pointing.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void Update() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
Debug.DrawLine(ray.origin, hit.point);
}
}
Direction? Do you mean the rotation? That solves the origin point, but how do I know which direction to point the ray and at what distance? There is no mouse action in my project, the movement is done with the wasd keys.
aye sorry. I believe Ray takes a vector3. So if the script is attached to the camera, then you could use Vector3.forward, and that will get the forward z direction of the camera.
Here is the right one. It has the direction, Vector3.forward, the object hit, and the distance. using UnityEngine; using System.Collections; public class Example : MonoBehaviour { void Update() { RaycastHit hit; if (Physics.Raycast(transform.position, Vector3.forward, out hit, 100.0F)) float distanceToGround = hit.distance; } }
Well I have a script attached to my camera, but the detection script is attached to the box collider. The box collider is a child of the camera and thats why it also rotates, but the detection script itself is not attached to the camera
And the box collider is an empty object, or that is attached the camera? If it's an empty gameObject, just make sure it's forward is the same as the camera. Essentially zero out its z and you should be good.
Direction? Do you mean the rotation? That solves the origin point, but how do I know which direction to point the ray and at what distance? There is no mouse action in my project, the movement is done with the wasd keys.
– aidenkaelaye sorry. I believe Ray takes a vector3. So if the script is attached to the camera, then you could use Vector3.forward, and that will get the forward z direction of the camera.
– jacobschellenbergHere is the right one. It has the direction, Vector3.forward, the object hit, and the distance. using UnityEngine; using System.Collections; public class Example : MonoBehaviour { void Update() { RaycastHit hit; if (Physics.Raycast(transform.position, Vector3.forward, out hit, 100.0F)) float distanceToGround = hit.distance; } }
– jacobschellenbergWell I have a script attached to my camera, but the detection script is attached to the box collider. The box collider is a child of the camera and thats why it also rotates, but the detection script itself is not attached to the camera
– aidenkaelAnd the box collider is an empty object, or that is attached the camera? If it's an empty gameObject, just make sure it's forward is the same as the camera. Essentially zero out its z and you should be good.
– jacobschellenberg