Using Raycasts for bullets and collision?

Hi guys,

I know this question has been asked a million times but I’m still pretty unsure how to get them working as even the example I have aren’t clear and are hard to apply to my situation. So if someone could break down what I have to do exactly it’d help a lot. I’m still unsure if I should go ahead with raycasts but from what I’ve read they’re my best bet.

I even had a look at the FPS tutorial but got a bit lost tbh…

Right now I have a gameobject called launcher attached to my player, a bullet prefab is attached to it. A this is the script:

public var secondsToDestroy = 5;

private var speed = 500;
var shotDelay = .5;

var projectile : Rigidbody;

function Start () {

while (true) {
while (!Input.GetKey(“space”)) yield;
// Fire bullet here

    var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );

instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
yield WaitForSeconds(shotDelay);
Destroy (instantiatedProjectile.gameObject, secondsToDestroy);

}

}

I have some asteroids set up which the player shoots at but I can’t get collision to work. I’m a bit unsure where I should add the actual script tbh (in the asteroid or launcher script).

And here is the script that sets up the asteroid field:

var thePrefab : GameObject;

var health = 5;

function Start () {

for( i = 0; i < 1000; ++i){
var instance : GameObject = Instantiate(thePrefab, Vector3(Random.Range(500,1500), Random.Range(-100, 200), Random.Range(500, 3500)), Random.rotation);
instance.transform.localScale = Vector3.one * Random.Range(8,20);

}

}

So that’s what I have so far. so I just wanted a little help with what I had to change and what I need to do exactly to get the ray cast up and running.

Thanks guys :slight_smile:

Maybe this will help: Raycast shooting - Questions & Answers - Unity Discussions
Learn to do a simple Google search before posting. This same question has been asked a million times.