Rayscast from the GameObject that is not the player does not work
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.
you can use raycast from everywhere, it is the raycast being shown? are you sure your objects are in layer -1? try just removing the -1 from the raycast, also do it the other way if you are looking for performance the raycast inside the getkeydown(space)
Your answer
Follow this Question
Related Questions
ClientRpc not functioning 0 Answers
It does not work removing items from the inventory 0 Answers
unable to attack enemy objects? 1 Answer
Changing an objects location 1 Answer
How to check if a player has passed a certain position?? 1 Answer