First Person Shooter raycast?

SO what im trying to do is have a raycast going out of my camera and if i shoot my gun, the bullet will go where the ray collides with a object. This is the code I got from somewhere.

function Update()
{

if(Input.GetMouseButton(0))

{
var ray = camera.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;

if(Physics.Raycast(ray,hit))
{

if(hit.transform == instruction)
{
inst = true;

}

if(hit.transform == start)
{

Debug.Log(“start”);
}

}

}

}

can someone tell me how i can implement this into my fps? thanks!

if(Input.GetMouseButton(0))

{
var ray = camera.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;

Will get you a raycasthit struct for the object you hit(if any).

if you want the gameobject just write hit.transform.gameobject (also do a check that something was actually hit). My way around javascript and nulls are not the best so someone else has to give you the answer for that

then when you found the gameobject you can do a lookup of its tag. gameobject.tag and do a check against say
if(hit.transform.gameobject.tag==“Enemy”)
{
// Damage the enemy here depending on your bullet.
}

yes but how do i add physics for example if i hit a box it will fly away or fallover the box it was on

Well first you need to add both a RigidBody and a Collider to a model of a box. Then when you do the raycast like so…

var hit = Physics.Raycast(…)

You need to add a force to whatever yo uhit

hit.rigidbody.AddForceAtPosition(hit.point, directionOfBullet * forceMagnitude);

Yeah but if i make the bullet go fast, it doesnt always get detected…

it should not matter how fast the bullet go since you do not actually shoot a physics object. You just shoot a ray and the ray should hit no matter what(as long as your ray is long enough).

HOw do i get the direct of the bullet? Also, WHy is my raycast facing down? how do i fix this?