how do i make a simple ray cast from a game object , store its collision information in a variable and then use that variable for use later… i have checked unity docs but it is not very clear… can someone please explain in simple code?
var ColliderHitByRaycast : Collider;
var GameObjectHitByRaycast : GameObject;
var HitCoordinates : Vector3;
function Update()
{
var ray = Camera.main.camera.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Input.GetButtonUp("Fire1"))
{
if (Physics.Raycast (ray, hit, Mathf.Infinity))
{
ColliderHitByRaycast = hit.collider;
GameObjectHitByRaycast = hit.collider.gameObject;
HitCoordinates = hit.point;
}
}
}
Well you can’t store the raycasts hit infromation, you can call it with var hit=RaycastHit;
And if you check the docs there is even an example Unity - Scripting API: Physics.Raycast
the 3 most used setups are shown in there.
good luck
function Update () {
// this var is the wonder that stores all information the raycast hits
var hit : RaycastHit;
//declair the raycast with te hit in it
if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
//and here they call the distance of the hit.
//you can also use if() statements to specify what the ray should listen to
//example : if(hit.collider.name=="cube"){
// execute this example }
var distanceToGround = hit.distance;
}
}