- Home /
How to get Raycast to detect collision with Object?
I know that similar questions have been asked but I've searched for a long time and still can't find any solutions.
Here is the code I am working with:
// pragma below is needed due to a UnityJS issue
#pragma strict
function Update(){
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider.Raycast (ray, hit,100.0)) {
Debug.DrawLine (ray.origin, hit.point);
}
}
What I want it to do is check to see if the raycast is hitting a specified object, instead of '100.0'. I know that to do this, I need a variable that holds the vector3 data of said object, then just replace '100.0' with that variable.
The problem is that I'm not so great with Javascript. Someone please help me out? (Sorry for the lengthy post)
Answer by ArkaneX · Aug 18, 2013 at 08:28 PM
is a length of a ray and it's not related to the object that will be hit by raycast. Calling collider.Raycast will only succeed, if you hit the game object running the above script, so you have the information which object has been hit.
If you need to use raycast which can hit different objects, then you should use Physics.Raycast, and use RaycastHit information to detect which collider was hit, using for example
hit.collider.tag == "sometag"
Answer by weenchehawk · Aug 18, 2013 at 08:18 PM
From my own personal stash
private bool IsTargetVisible()
{
RaycastHit ObstacleHit;
if (Objective) // make sure we have an objective first or we get a dirty error.
return (Physics.Raycast(OurShip.position, Objective.position - OurShip.position, out ObstacleHit, Mathf.Infinity) && ObstacleHit.transform != OurShip && ObstacleHit.transform == Objective);
else
return false;
}
Sorry, it might not have been obvious, this shoots a raycast out from the forward direction of your Ship, if it collides with your Objective (which you need to set) it returns true.
Answer by DAXONATER · Aug 19, 2013 at 02:04 AM
Thanks for the answers, but none of them really fit what I'm trying to do. I'll be a little more specific this time :P
I'm trying to make a script that creates a raycast in front of the player, and if the ray hits the enemy, the debug log will print something out. Basically, how can I get a ray to check for a collision with an object.
EDIT: This script here seems to be a bit more managable.
function Update() {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
if (hit.collider.gameObject.find("wormyguy")) {
Debug.Log("Success");
}
}
}
The only problem is that the ray shouldn't be coming from the mouse position (It wouldn't be accurate due to the sensitivity of the mouse) , It would be great if I could get it to come from an empty object, is there anyway to do that?
(the "wormyguy" is just a temporary name for the object) Sorry for the textbook length reply again
Firstly, it's not an answer, and should be a comment to your original question.
Secondly, I believe you don't really know how the raycast work. In your example ray doesn't come from mouse position, but from camera position. If you want the ray to be created from some object in some direction (like in front of player), then you can use Physics.Raycast method providing ray start position and direction of the ray. Please take a look at my answer to this question.
EDIT: length of the post is not a problem. The more we know, the better we can help.