Hi, I’m having a trouble understanding why this doesn’t work. The Debug Ray show correctly that the ray is going from point a to point b all the time, however doesn’t detect any collision.
#pragma strict
var player : GameObject;
var Range = 5;
var Layer : LayerMask;
var Name = "";
function Start () {
}
function Update () {
var hit : RaycastHit;
var rayDirection = player.transform.position;
if(Physics.Raycast(transform.position, rayDirection, hit, Range, Layer)){
Name = hit.collider.gameObject.name;
Debug.Log(hit.collider.gameObject.name );
if(hit.collider.gameObject.name == "Test_Subject" && hit.distance < Range){
//DoSomething
}
}
else{
//DoNothing
}
Debug.DrawLine(transform.position, rayDirection, Color.blue);
}
And yes I did try to put rayDirection to, however then it goes towards the sky…
I’m trying to make the raycast constantly conneted towards the player, however only in the range of 5. So when he leaves it it will do something, when he is in the range, he will do something too
Debug.DrawLine takes a start and end point, while a raycast takes start and direction (actually what @LeftyRighty already pointed out).
A direction can be calculated by subtracting the origin from the end.
directionVector = endVector - startVector, so your direction should be player.transform.position - transform.position.
[quote=“Suddoha, post:5, topic: 597325, username:Suddoha”]
Debug.DrawLine takes a start and end point, while a raycast takes start and direction (actually what @LeftyRighty already pointed out).
A direction can be calculated by subtracting the origin from the end.
directionVector = endVector - startVector, so your direction should be player.transform.position - transform.position.
[/quote]
Like I said
You can only know that by (still) using the same for both, the raycast and DrawLine method.
Internally they might work the same, but they take different arguments. One takes a direction, the other one a position. Both are vectors, but a position vector is not necessarily a direction vector and vice versa, unless your reference point is the origin.
There’s also DrawRay, maybe you’ll be fine with that.
A small example, the following happens if you pass start and player pos like you do:
Consider Start (1, 2, 3) Player (5, 2, 1)
What Debug.DrawLine does: draws a ray from (1, 2, 3) to (5, 2, 1) as it’s supposed to.
What the raycast does, if you use the player position as direction: simply said, it casts a ray from (1, 2, 3) through (1, 2, 3) + x*(5, 2, 1).
Some sample points:
x=0 : (1, 2, 3), x=1: (6, 4, 4), x=1,5: (8.5, 5, 4.5), x=2: (11, 6, 6).
In other words, try to solve (5, 2, 1) = (1, 2, 3) + x*(5, 2, 1) and you’ll see that doesn’t work.
I edited your script a bit in order to show the use of Raycast, DrawLine and DrawRay.
DrawRay behaves like Raycast, it takes start position and direction, DrawLine, as stated, takes start and end point.
You can switch from DrawRay to DrawLine and vice versa using the checkbox for the variable b in the inspector with this script: I’m usually programming in C# but this should work, at least it does when I use it.
#pragma strict
var player : GameObject;
var Range : int = 5;
var Layer : LayerMask;
var Name = "";
// toggle DrawLine, DrawRay via inspector
var b : boolean;
function Update ()
{
var hit : RaycastHit;
// direction for the raycast
var rayDirection = player.transform.position - transform.position;
if(Physics.Raycast(transform.position, rayDirection, hit, Range, Layer))
{
//Debug.Log(hit.collider.name );
if(hit.collider.name == "Test_Subject" && hit.distance < Range)
{
Debug.Log("hitting the test subject");
}
}
// just to show the usage of both
if(b)
Debug.DrawLine(transform.position, player.transform.position, Color.blue);
else
Debug.DrawRay(transform.position, rayDirection, Color.red);
}
Not sure how your collider looks like, but you probably don’t wanna raycast to his feet. You should think about an offset of about 1 unit (assuming your character is 2 units tall).
Here is some code. You put this on object one, and then you drag the target object into the targetObject slot.
Note - this script is C#
Also, you set up the layermask by selecting the layers you want to ignore. If you dont want to do it that way, then just remove the ~ sign in the layermask.
Click for code
using UnityEngine;
public class RayCheckRange : MonoBehaviour
{
public Collider targetObject;
public float range = 5f;
public LayerMask ignoreLayers;
void Update()
{
Vector3 direction = (targetObject.transform.position - transform.position).normalized;
Debug.DrawRay(transform.position, direction * range);
RaycastHit hitInfo;
if(Physics.Raycast(transform.position, direction, out hitInfo, range, ~ignoreLayers))
{
if(hitInfo.collider == targetObject)
{
Debug.Log("Hit");
}
}
}
}
If that isnt what you need, then please explain in more detail what it is you are trying to do =).
Thank you for your contribution but C# code is not so useful to me, as my code needs to link to antoher code, and that code to the third code. However I did mange it somehow to work, if I run into more issues I’ll be sure to come by.
Thanks to all once again, and to you HiddenMonk for this code on C# I think I can use it just by getting the idea how it is constructed.
One thing i feel raycast is doing wrong. Ignoring layers.
Yes it ignores layers but it stops there. To give a example of what I mean by that:
Ray -------> [Layer:Water] [Layer: Player]
What I mean is that ray stops if it hits water, yes ignoring it, however I need to pass trought water, so it can detect player
Ray -------> [IGNORE:Layer:Water] --------> [Layer: Player]