Blaster script help please.

So I will start off by saying that I am very new to this, have been using Gamer to developer to try to get a grasp on this. Somewhere a long the way, I had to mess up doing this script to make my game object execute if it hits another object. So this is what I got so far, please let me know where I messed up.

/// <summary>
/// This script is attached to the Blaster projectile and it
/// governs the behaviour of the projectile.
///
/// </summary>


public class BlasterScript : MonoBehaviour {

    //Variables Start

    //A quick reference.

    private Transform myTransform;

    //The projectiles flight speed.

    private float projectileSpeed = 10;
    //Prevent the projectile from causing
    //further harm once it has hit something.

    private bool expended = false;

    //A ray projected in front of the projectile
    //to see if it will hit a recognisable collider.

    private RaycastHit hit;

    //The range of that ray.

    private float range = 1.5f;

    //Variables End

    void Start ()
    {
        myTransform = transform;
    }
   
    // Update is called once per frame
    void Update ()
    {
        //Translate the projectile in the up direction the pointed
        //end of the projectile.

        myTransform.Translate (Vector3.up * projectileSpeed * Time.deltaTime);
   
        //If the ray hits something then execute this code.

        if(Physics.Raycast(myTransform.position.myTransform.up. out hit. range) &&
           expended == false)
        (
             //If the collider has the tag of Floor then..

                if(hit.transform.tag == "Floor")
                (
                expended = true;

                    //Make the projectile become invisible.

                myTransform.renderer.enabled = false;

                //Turn off the light. The halo will also disappear.

                myTransform.light.enabled = false;
                )
        )

Im unclear about what your problem is? Whats this not doing that you want it to do?

My apologize, I am trying to get the blaster/ray to basically turn invisible after colliding with another game object. If that makes any sense? Really just want to make a blaster script that shoots, collides, disappears.

oh okay, well if the blaster is like a bullet or projectile you are shooting then i think your close but missing some key elements.

First the projectile should be made a prefab.
Then you would need a public variable reference to that prefab.
I would use Instantiate to create the prefab at the desired location and add force in the direction you want.
When the prefab collider hits another collider, i would use something like onTriggerEnter and destroy the prefab.

Sorry i don’t have any solid code written up, but that should help guide you in the direction you need to go.

1 Like

Okay, thank you Steven, sorry it took so long to reply. barely had time to get on my Unity.

I did have the projectile/blaster as a prefab, but I don’t think I set the public variable. Will give it a try.