- Home /
raycast screenpointtoray isnt working for some reason
I have a script on the main camera with raycast using screenpointtoray, u click on a gameobject in the scene and it detects it fine. I am trying to use the exact same thing on a script on my controller empty gameObject, to detect if i touch something in the scene and it wont work, at all.
if(Input.GetMouseButton(0) && jumpState == 0)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.transform == jumpScreen)
{
Debug.Log("jumpScreen");
dude.animation.Play("jump");
audio.clip = jumpGrunt;
audio.Play();
ySpeed -= jumpForce;
jumpState = 1;
}
}
}
Just want the player to touch left side of phone screen to make character jump, jumpScreen is a game object with a collider on left side of camera, mesh renderer turned off.
Does Camera.main.ScreenPointToRay only work if its on a script thats on a camera?
I could really use some help with this. Using Camera.main.ScreenPointToRay(Input.mousePosition); seems to only work when its on the camera.
Looks reasonable to me. Is that in your Update() method? You might add some more debug statements to make sure that the loop is being hit.
yeah, in the code there I am using Debug.Log and its not co$$anonymous$$g up in the console window. I am at a loss of why its not working. I am using this function to do something else and that script is on the main camera and it works fine.
Is the raycast hitting anything? What is the raycast hitting?
if(Physics.Raycast(ray, out hit))
{
Debug.Log("Ray hit : " + hit.collider.gameObject.name);
if(hit.transform == jumpScreen)
// ....
apparently not. I dont know why though. The object has a box collider on it and tagged correctly
Your answer
Follow this Question
Related Questions
Is this way of making a raycast correct 2 Answers
Problem with Unity Ray Trace AI Detect Player 0 Answers
Raycast returns null for no apparent reason 0 Answers
Raycast not hitting instantiated object when timescale is 0 1 Answer
Ray is not being detected 0 Answers