Hi guys,
I’ve been on google all night and I’ve just been getting more and more confused so thought I’d ask for some help.
Basically I have a game object with this script:
public var secondsToDestroy = 5;
private var speed = 500;
var shotDelay = .5;
//var instantiatedProjectile : Rigidbody;
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);
}
}
and this essentially launches a bullet prefab that’s been attached to the script.
Now I’ve been trying to set up collision using raycasts which is going okay…
So I have this script attached to the bullets:
function Update () {
var forward = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.position, forward * 10, Color.green);
if(Physics.Raycast(transform.position, forward, hit, 10)){
Debug.Log("Hit");
if(hit.collider.gameObject.name == "Asteroid(Clone)"){
Debug.Log("asteroid destroy!");
//Code to destroy bullet
//Code to destroy asteroid
//Destroy (this);// not working :(
}
}
}
So that’s all the shooting code really. I’ve done a lot of research but nothing too clear and a lot is really confusing as I’m new to unity so if someone could point me in the right direction I’d be really grateful.
just everything in enter to destroy the bullet or asteroid doesn’t work. I’m not sure if it’s because it’s a prefab and I have to somehow reference the specific clone to destroy it or something else. I was thinking that maybe I should make the asteroid and bullet global values so I could access them from other script but I wasn’t too sure how tbh…
Also here’s the Asteroid script if it helps at all.
var thePrefab : GameObject;
var asteroidCount = 0;
function Start () {
for( i = 0; i < asteroidCount; ++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);
}
Thanks again guys. Really trying to figure this out on my own but I guess every now and again you need a point in the right direction…