Unity 4 Raycast Broken.

So, I got this code, and according to the API it should work… The ray casted is meant to be infinitely long, but instead its like 0.00000000000000001 long.

enter code hereusing UnityEngine;
using System.Collections;

public class Raycast : MonoBehaviour {

	void Update(){
		Vector3 fwd = transform.TransformDirection(Vector3.forward);
		RaycastHit hit;

		if(Physics.Raycast(transform.position, fwd, out hit)){
			if(hit.collider.gameObject.tag == "Vehicle"){
				print ("Enter Vehicle");
				if(Input.GetKeyDown(KeyCode.E)){
                   //Do something if this would work
				}
			}
		}
	}
}

2 Answers

2

using layer mask will make ray to ignore some layers

LayerMask raymask=~(1 << 8); //this will exclude layer 8 so put your player to layer 8

    if(Physics.Raycast(transform.position, fwd, out hit,Mathf.Infinity,raymask))

Doesnt work, heres a screenshot of the ray after i did Debug.DrawRay(transform.position, fwd);. Heres a link : http://i.imgur.com/TYnmpB9.png

add a time for its existence to see the ray so check this Debug.DrawRay(transform.position, fwd,Color.red, 5.0f)

Hmm, lines are still the same length, heres a new pic : http://i.imgur.com/IJsQUDS.png

where you do the debug.drawray dont do it in your if(Input.GetKeyDown(KeyCode.E)) it will be a problem

I dont, i do it above it using UnityEngine; using System.Collections; public class Raycast : MonoBehaviour { void Update(){ Vector3 fwd = transform.TransformDirection(Vector3.forward); RaycastHit hit; if(Physics.Raycast(transform.position, fwd, out hit, Mathf.Infinity)){ Debug.DrawRay(transform.position, fwd,Color.red, 5.0f); if(hit.collider.gameObject.tag == "Vehicle"){ print ("Enter Vehicle"); if(Input.GetKeyDown(KeyCode.E)){ } } } } }

For Debug.DrawRay the direction vector is what determines the distance of the ray. Since the values for Vector3.forward are (0,0,1) it will only shoot 1 unit out. You can multiply it by a scalar value to (Vector3.forward * 5) to increase the distance.

For Physics.Raycast the length of the raycast is determined by the distance parameter which can be found in some of the variations of the Physics.Raycast method. If no distance is specified then the distance is equal to Mathf.Infinity like @Bunny83 pointed out.

You can use Physics.Raycast(Ray ray, out RaycastHit hitInfo, float distance) version and get the ray from doing Ray ray = m_camera.ScreenPointToRay(centerOfScreen); and see if that works for you.

I multiplied fwd by 10, and it seems that the range of the debug has changed, but not the range of the actual ray..

Never mind. It does work, guess it was just glitching for me! thanks for your help.

@ThePunisher: Sorry, but that's simply not true. The direction vector is actually normalized by Unity and the length of that vector doesn't matter. As you can see in [the docs][1] when you use the version without an explicit distance it defaults to Mathf.Infinity So the ray is actually infinitely long. Debug.DrawRay doesn't have a length parameter and it is using the length of the direction vector provided. [1]: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Hey Bunny, You're right, the direction vector is normalized by Unity and scaling it does not affect the raycast's length. My question is how did he get that to work? lol. Edit: Oh I know why I told him to multiply the forward vector by a scalar value. I saw that he was using Debug.DrawRay in his screenshots and mixed the two.

I updated the answer to remove misinformation.