As it says. Can anyone help? It isn’t an issue with the code, because I’ve checked that the ray is set to hit the mesh collider. Doesn’t do anything.
if ( Physics.Raycast( transform.position, -transform.up * 500, 0 ) )
{
Debug.Log("We hit it.");
}
As it says. Can anyone help? It isn’t an issue with the code, because I’ve checked that the ray is set to hit the mesh collider. Doesn’t do anything.
if ( Physics.Raycast( transform.position, -transform.up * 500, 0 ) )
{
Debug.Log("We hit it.");
}
Even if I remove the 0 from the layermask, it returns nothing. At all.
Are you trying to shoot the ray down for 500 units? The second parameter is simply the direction of the ray and should only be the direction. The third parameter is the actual distance. Setting it to zero will have it do nothing.
if ( Physics.Raycast( transform.position, -transform.up, 500 ) )
It says according to the API, here; Unity - Scripting API: Physics.Raycast
For the first thing, that the second parameter is end, and the fourth is the layerMask.
Also, that still doesn’t fix it.
if ( Physics.Raycast(transform.position, -transform.up, 400F, 500) )
{
Debug.Log("We hit it");
}
If you are specifying the values for parameters, it will only use the default values for parameters if there isn’t a parameter specified in that slot. If you want to use the fourth parameter, you have to specify all four.
Additionally if this still isn’t working, make sure the origin isn’t accidentally inside of the collider you’re trying to check for. It ignores any colliders it starts in.
if ( Physics.Raycast( transform.position, -transform.up, 500, 0 ) )
I said I tried that and it still doesn’t work.
Is your layer mask supposed to be 500? Because that’s what you specified in your last code chunk.
Okay, one question; why is there all these different ways according to the API to set the parameters instead of just one? All this does is cause confusion.
The alternative would have been overloaded functions.
It doesn’t work even now with
if ( Physics.Raycast ( transform.position, -transform.up , out RayInfo, 500 ) )
{
Debug.Log("We hit it");
}
My current scene for testing is a plane with a sphere about two units above with the scripts attached. So far I’ve had no problems getting them to work. This is mostly the example from the Unity docs aside from adjusting the distance.
using UnityEngine;
using System.Collections;
public class Foo : MonoBehaviour {
private RaycastHit hit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Physics.Raycast(transform.position, -transform.up, out hit, 500))
Debug.Log("It's a hit!");
}
}
I tried that too with a different mesh, it works for that mesh but not this one.
How close is the non-working mesh to the collider you want to find?
What do you mean how close is the collider? It’s a mesh collider.
You want the mesh to detect if it hits another surface, right? If the ray starts within a collider, it won’t find that collider. Is the working mesh farther away from the mesh you are raycasting to find?
I want the ray to detect if it hits the mesh collider the mesh is attached to.
The way I do the ray is it isn’t attached to a collider. IT’s just attached to an empty gameObject.