Skip to content
Tags

Acess Item and Get trigger activate via raycast function.

September 3, 2010

 This section has contain about raycast function.

below is script.

static var GRENADE_AMMO = 0;

function OnControllerColliderHit(hit : ControllerColliderHit)

{

if(hit.gameObject.tag == “createGrenades”)

{

//destroy the ammo box

Destroy(hit.gameObject);

//add ammo to inventory

GRENADE_AMMO += 8;

print(“YOU NOW HAVE “+ GRENADE_AMMO +” GRENADES”);

GameObject.Find(“g_Count”).guiText.text = “”+GRENADE_AMMO;

}

}

var rayCastLength = 5;

function Update()

{        

var hit : RaycastHit;

//check if we’re colliding

if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))

{

//…with a door

if(hit.collider.gameObject.tag == “door”)//Acess GameObject name = door

{

//open the door

hit.collider.gameObject.animation.Play(“door_open”);//get acess animation list “door_open” that is animation event trigger!

}

}

}

Physics.Raycast

REF. from Help

static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

Parameters

origin The starting point of the ray in world coordinates.
direction The direction of the ray.
distance The length of the ray
layerMask A Layer mask that is used to selectively ignore colliders when casting a ray.

Returns

bool – True when the ray intersects any collider, otherwise false.

Description

Casts a ray against all colliders in the scene.

function Update () {

var fwd = transform.TransformDirection (Vector3.forward);

if (Physics.Raycast (transform.position, fwd, 10)) {

print (“There is something in front of the object!”);

}

}

//script end

static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

Parameters

origin The starting point of the ray in world coordinates.
direction The direction of the ray.
distance The length of the ray
hitInfo If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
layerMask A Layer mask that is used to selectively ignore colliders when casting a ray.

Returns

bool – True when the ray intersects any collider, otherwise false

Advertisements

From → Unity3D Study

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: