- Home /
Draw a ray to deterime if anything is in front of colliding object?
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.
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":
Answer by jacobschellenberg · Nov 11, 2013 at 09:14 PM
Here is an example on the Unity references.
Attach a script to the camera itself and use the direction of the camera to determine where the Ray is pointing.
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
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 : $$anonymous$$onoBehaviour {
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.
Answer by Sekto · Nov 12, 2013 at 01:58 AM
// C# Code
public float distance;
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
{
distance = hit.distance;
// now you now how far away you are from an object. With the hit variable you basically have a pointer to game object.
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613121649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why Isn't My Raycast Working? 3 Answers
3rd Person camera collision 2 Answers
Melee-like function? 1 Answer
How To Invoke RayCastAll? 1 Answer