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.

This is a very vague question. http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html http://docs.unity3d.com/Documentation/ScriptReference/Input.html http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

–

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...

–

1 Answer

1

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.

–