- Home /
Raycasting is not working!
Here is my code and Im not sure what I'm doing wrong. This is the first time raycasting isnt working for me! Here:
void Update ()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 30f) && Input.GetMouseButtonDown(0))
{
if(hit.collider.gameObject.tag == "Key" == "Key")
{
Debug.Log("Grabbed The Key");
}
}
}
EDIT: Just to let you know, this is attached to a first person controller that comes with the Unity Standard Assets.
One problem is the use of Vector3.forward
. That is the world forward. Likely you want to use transform.forward
which is the direction the game object this is attached to is facing.
Answer by YoungDeveloper · Aug 14, 2013 at 12:44 AM
Hi, If you are trying to cast ray from players camera center use this.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Note that cursor must be locked, if it isn't for you, here's the code
Screen.lockCursor = true; //must be in Update function;
Is debug showing the message ?
void Update ()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 30f)) Debug.Log("Hit");
}
And by the way, It's
hit.collider.gameObject.tag == "$$anonymous$$ey"
not
hit.collider.tag == "$$anonymous$$ey"
void Update ()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 30f){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)){ // left mouse click
if(hit.collider.gameObject.tag == "$$anonymous$$ey")
Debug.Log("Grabbed The $$anonymous$$ey");
}
}
It's because of this line
Ray ray = new Ray(transform.position, transform.forward);
Where do you want to shoot the ray from ?
Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width/2,Screen.height/2));
Answer by deadfish90 · Aug 14, 2013 at 01:59 AM
What others are mentioning about the forward facing vector not changing with rotations is true if you have it attached to the "First Person Controller" game object. That game object only rotates in Y axis. What you should do is attach the script with the ray casting on the camera, whose rotation occurs in more than just one axis. Below is the code that I had working. Aside from that, I've added a gizmo drawLine to show you what happens when the script is attached to the "First Person Controller" game object versus the camera.
void Update ()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 30f) && Input.GetMouseButtonDown(0))
{
if(hit.collider.gameObject.tag == "Key")
{
Debug.Log("Grabbed The Key");
}
}
}
void OnDrawGizmosSelected() {
Gizmos.color = Color.blue;
Gizmos.DrawLine(transform.position, transform.position + (transform.forward * 30));
}
PS, you had
== "Keys" == "Keys"
in your if statement. I'm pretty sure that was a typo, but I thought I'd mention it just in case.
Your answer
Follow this Question
Related Questions
Locate a game object using rays 1 Answer
Don't allow raycasts to go through colliders? 3 Answers
Click&Drag Misterious Disappearing! 1 Answer
I don't know why i can't detect by ray sth. tagged, 1 Answer
Physic Collider Test Rotation 0 Answers