- Home /
The raycasthit does not see the cillider when hitting it
In my game if the player presses "E" while looking at the computer. The pointer on that screen inside the game is made with a simple 3d object. The mode is changing when pressing E and now the arrows manage the move of the poitner on the screen. I am using the same simpe method of raycasting to press on the computer and to press on the button on the screen using the pointer. button on the screen is also a 3d object with the collider. The raycasthit from the player can determine the buttons on the screen as a collider however the pointer can't. This is the code for the pointer using System.Collections; using System.Collections.Generic; using UnityEngine;
public class pointerMove : MonoBehaviour
{
public bool allowMove = false;
public float speedMove = 0.1f;
public GameObject thisPointer;
RaycastHit whatIHit;
void Start ()
{
}
void Update ()
{
if (allowMove == true)
{
if (Input.GetKey(KeyCode.DownArrow))
{
//thisPointer.transform.position = new Vector3(speedMove*Time.deltaTime,0f,0f);
transform.Translate(0,-speedMove * Time.deltaTime, 0);
}
if (Input.GetKey(KeyCode.UpArrow))
{
//thisPointer.transform.position = new Vector3(speedMove*Time.deltaTime,0f,0f);
transform.Translate(0,speedMove * Time.deltaTime, 0);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
//thisPointer.transform.position = new Vector3(speedMove*Time.deltaTime,0f,0f);
transform.Translate(speedMove * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.RightArrow))
{
//thisPointer.transform.position = new Vector3(speedMove*Time.deltaTime,0f,0f);
transform.Translate(-speedMove * Time.deltaTime, 0, 0);
}
//if the raycast hits the collider
if (Physics.Raycast(this.transform.position, this.transform.forward, out whatIHit, -1))
{
Debug.DrawRay(this.transform.position, this.transform.forward * (-1), Color.magenta);
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("I touched " + whatIHit.collider.gameObject.name);
whatIHit.collider.gameObject.GetComponent<ButtonParameters>().pressed = true;
}
}
}
}
}
at "if (Physics.Raycast(this.transform.position...." the program stops which I determined with debug log. The same method applied for player and works with objects. However, when I ut the script on the pointer(and obviously change sligtly some parts but not the raycasthit part) - nothing works. Maybe the raycast only allowed from the camera? However the ray is produced i can see it... ( The bool allowMove obviosly changes from the different script which does not affect anything alse on this script.
Answer by Eno-Khaon · Jan 21, 2019 at 08:49 PM
You're giving your Raycast -1 range, which does not mean infinity, nor does it mean 1 range backwards. The first Raycast collision test will determine that it's gone beyond the maximum range and will stop there.
if(Input.GetMouseButtonDown(0))
{
RaycastHit testHit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out testHit, -1))
{
// Never triggers
Debug.Log("I hit with -1 range");
}
else if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out testHit, 100))
{
// Reliably triggers
Debug.Log("I hit with 100 range");
}
}
Answer by sujitmarcus · Jan 22, 2019 at 12:02 PM
Increase the Raycast Hitting Range.
if (Physics.Raycast(this.transform.position, this.transform.forward, out whatIHit, 1000f))
{
Debug.DrawRay(this.transform.position, this.transform.forward * (-1), Color.magenta);
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("I touched " + whatIHit.collider.gameObject.name);
whatIHit.collider.gameObject.GetComponent<ButtonParameters>().pressed = true;
}
}