- Home /
interacting with a raycast
alright im having some trouble on how to interact using the e key with a raycast anyone help me get started on hte raycast and input? i can do the functions no problem.
ok i understand the input.getbuttondown(keycode.E)but ive looked and i cant for the life of me figure out the raycast or where to implement the input string...
Answer by AlucardJay · Aug 21, 2012 at 07:49 AM
Like I said in the comments, this is a very vague question. Without context, it's hard to give an accurate answer. For starters, are you raycasting from the mouse position, or from a gameObject in the scene? Here is a generic answer using the Camera.main.ScreenPointToRay method :
#pragma strict
function Update()
{
// -- Check if the E key has been pressed down on this frame --
if ( Input.GetKeyDown( KeyCode.E ) )
{
// -- Key has been pressed, now Raycast --
// variables used in raycast
var mouseRay = Camera.main.ScreenPointToRay( Input.mousePosition );
var rayHit : RaycastHit;
// raycast command
if ( Physics.Raycast( mouseRay, rayHit, 1000 ) )
{
Debug.Log( rayHit.collider.gameObject.name );
}
}
}
place a cube, capsule, sphere and cylinder in an empty scene. Attach this script to an empty gameObject (or the camera, or to one of the objects). Hit play.
When you hover the mouse over an object and press E , the name of the object should appear in the console.
Your answer
Follow this Question
Related Questions
Move player with mouse help 0 Answers
GuiTexture as crosshair as scroll over to activate things 1 Answer
having problems with my raycast shooting script 0 Answers
[SOLVED] Only instantiating once 1 Answer