Raycasting Not Working
This is my code:
public class GroundCheckScript : MonoBehaviour {
public float rayLength = 0.75f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void FixedUpadte()
{
Ray2D groundAlign = new Ray2D(new Vector2(gameObject.GetComponent<Transform>().position.x, gameObject.GetComponent<Transform>().position.y), Vector2.down);
Debug.DrawRay (new Vector2(gameObject.GetComponent<Transform>().position.x, gameObject.GetComponent<Transform>().position.y), Vector2.down * rayLength);
}
}
...And no matter what Game Object I add it to, it doesn't cast the ray, or at least, I can't see it. What am I doing wrong?
The ray length is pretty short are you sure you can see that? Also it's white by default make it red or something. Other than these i don't see why it shouldn't work. Btw this is simpler and more efficient: new Ray2D(transform.position, Vector2.down);
Answer by DCordoba · Jan 12, 2016 at 09:40 AM
from http://docs.unity3d.com/ScriptReference/Debug.DrawRay.html
first I dont see any example using a vector2 but because not trows an error to compile should work, second you not set up duration, by default is 0.0f i.e. is rendered only 1 frame, so if you not have a very slow frame rate or becomes vampire you not be able to see it. please set a duration (expresed in seconds), for rare reasons you cant define duration without define color, so finally is like this, for a ray for 2 seconds
Debug.DrawRay (new Vector2(gameObject.GetComponent<Transform>().position.x, gameObject.GetComponent<Transform>().position.y), Vector2.down * rayLength, Color.white, 2.0f);
One more thing: void FixedUpadte() is spelled wrong. It's FixedUpdate().
Your answer
Follow this Question
Related Questions
Raycast Reflection 2D 1 Answer
Raycast not detecting? 0 Answers
raycast2d problem enemies,Raycast2D problem 0 Answers