For some reason, I can’t get raycast to work. Can someone show me a proper script for raycast?
come on! Anyone?
Have you checked the Docs (I have a feeling no and you want someone to give you the code)?.. Look up Physics.Raycast and there is code there.
Sorry to sound harsh, but you did bump your post under an hour of originally posting.
Failing that, post what code you have been trying and we’ll try and fix it.
Regards,
Matt.
C# or UnityScript?
C#:
private RaycastHit hit;
void Update (){
if(Physics.Raycast( transform.position, -Vector3.up, out hit, 15 ))
{
Debug.Log("HIT!");
}
}
UnityScript / javaScript:
var hit : RaycastHit;
function Update () {
if (Physics.Raycast (transform.position, -Vector3.up, hit))
{
Debug.Log("HIT!");
}
}
Hope that helps
I grabbed the UnityScript / javascript from the help files btw
it works! But can you explain what goes into the part where you put -Vector3.up?
bump
Vector3.up is shorthand for (0,1,0), in that case it is the direction the ray is to be cast in.
Here’s an example I’m using for an aim assist script
if (Physics.Raycast(fpCamera.transform.position, fwd, hit, aimFarthestPoint))
fpCamera.transform.position is where the ray is to originate from
fwd is a variabe to define it’s direction (var fwd = transform.TransformDirection(Vector3.forward); )
hit is the point in space where the ray hits something (var hit : RaycastHit;)
And aimFarthestPoint is the total length of the ray (public var aimFarthestPoint : int = 100;)
Remember the scripting reference is your friend ;]
THANK YOU SO MUCH!