Shooter - Bullet Tracers finally working

in #unity6 years ago (edited)

Tracer Test 2.mkv_snapshot_01.30.jpg

https://www.bitchute.com/video/hUUZySh7wA6Q/

I've done it! After going through a ton of documentation and forum posts I gave up and finally made a thread where I was determined to solve this problem once and for all. Awhile back I had attempted to make tracers like this work with my raycast code and I had failed miserably however, there was a helpful guy on Unity I was chatting to that explained various things to me and it turned out I had completely missed a trick with this sort of code and that immediately fixed everything.

As you will see from the video and the screenshot, it works damn near perfectly, the tracers go exactly to the where the bullet holes are and even fit snugly in them due to the way I've done the streaks. The only issue is the speed of the tracers and I think the style as well, I'll need to do more tweaking on that but it will just be down to the settings of the actual particles and so on rather than anything to do with the code.

This is fantastic though because it means I will have all sorts of options for my weapons open up to me, do you guys remember the needlers from Halo and weapons like that? Yep, you guessed it, I can do shit like that now if I want to I'm not limited to just simply using empty gameobjects now so literally anything is possible FPS wise and that's exactly what I was hoping for.

Expect more experimentation with all kinds of crazy tracer effects in the future, by the way, as an added bonus since the code is finished I thought it would be nice to post it up for anybody wanting to mess with tracers generally, it can be a real pain in the arse and there isn't very many tutorials on it but here you go. The magic code here is the GetComponent, I had no idea you could use this code in reverse, but it turns out you can. This is purely for programmers but I was told in order to make this work I would have to make a public variable on a script on the actual tracer, then you grab the public variable there and assign it to the raycast. What I was trying to do which was wrong, was the other way round where I was trying to grab the raycast from the script that was attached to the tracer.

Hope that makes sense, here's the code, the actual shooting code is attached to an empty gameobject on the muzzle.

Raycast Shoot


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunHit

{
    public float damage;

    public RaycastHit raycastHit;
}

public class RaycastShoot : MonoBehaviour {


    public float fireDelay = 0.6f;
    public float maxBulletSpreadAngle = 15.0f;
    public float damage = 1.0f;
    public float timeUntilMaxSpreadAngle = 1.0f;
    public float ExplosionForce = 1.0f;
    public float ExplosionRadius = 3.0f;

    public GameObject bulletHolePrefab;
    public float bulletHoleSizeWidthMin = 0.001f;
    public float bulletHoleSizeWidthMax = 0.003f;

    public GameObject tracerParticle;

    private bool readyToFire = true;
    private float fireTime;

    public float speed = 1.0f;

    public Vector3 destination;

    // public float speed = 1.0f;

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButton (0)) 
        
        {

            fireTime += Time.deltaTime;



            if (readyToFire) 
            
            {

                Vector3 fireDirection = transform.forward;
                Quaternion fireRotation = Quaternion.LookRotation (fireDirection);
                Quaternion randomRotation = Random.rotation;

                float currentSpread = Mathf.Lerp (0.0f, maxBulletSpreadAngle, fireTime / timeUntilMaxSpreadAngle);

                fireRotation = Quaternion.RotateTowards (fireRotation, randomRotation, Random.Range (0.0f, currentSpread));
            
                RaycastHit hit;

                if (Physics.Raycast (transform.position, fireRotation * Vector3.forward, out hit, Mathf.Infinity)) 
                
                {
                    GunHit gunHit = new GunHit ();
                    gunHit.raycastHit = hit;

                    readyToFire = false;
                    Invoke ("SetReadyToFire", fireDelay);
                    Instantiate (Resources.Load ("BulletImpactParticle"), hit.point, Quaternion.LookRotation (hit.normal));

                    Instantiate (bulletHolePrefab, hit.point, Quaternion.FromToRotation ( Vector3.up, hit.normal ) );
                    bulletHolePrefab.transform.localScale = new Vector3 ( 0.3381936f, 0.8108215f, 0.03285804f ) * Random.Range ( 0.5f, 2f );



                    GameObject tracerParticleObject = Instantiate ( tracerParticle, transform.position, transform.rotation ) as GameObject;
                    tracerParticleObject.GetComponent<moveTowardsBulletTracer>().destination = hit.point;


                    FracturedChunk chunkRaycast = FracturedChunk.ChunkRaycast (transform.position, transform.forward, out hit);

                    if (chunkRaycast) 

                    {
                        chunkRaycast.Impact (hit.point, ExplosionForce, ExplosionRadius, true);
                    }

                    //



                }


            }

        } 

        else
        
        {
            fireTime = 0.0f;
        }

    }



    void SetReadyToFire ()

    {
        readyToFire = true;
    }





}

moveTowardsBulletTracer


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moveTowardsBulletTracer : MonoBehaviour {

    public Vector3 destination;

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {

        transform.position = Vector3.MoveTowards ( transform.position, destination, 20 * Time.deltaTime );

    }
}

Edit: Hmm, code blocks seem to be buggy, hope the Steemit devs fix it.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64420.25
ETH 3150.23
USDT 1.00
SBD 3.99