First time poster needs help combing Codes

Hy all. so. ima just say it. I’m really bad at coding so I figured why not ask the community for help instead of giving up.

here is the issue. I’m using Brackey’s “Shooting with Raycasts - Unity Tutorial” as a base but I want to replace his standard shooting with World of Zero’s “Better Projectiles with Gravity and Trajectory Prediction”. I have these both working individually but I don’t know how to get my gun to shoot Zeros projectiles.

here is Brackey’s script

using UnityEngine;

public class GunScript : MonoBehaviour
{
    // Start is called before the first frame update
    public float damage = 10f;
    public float range = 100f;

    public Camera fpsCam;
 
        // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }

    void Shoot ()
    {
        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            Debug.Log(hit.transform.name);

            Target target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
                target.TakeDamage(damage);
            }
        }
    }
}

here is Zero’s

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

public class ProjectileScript : MonoBehaviour
{
    public float speed;
    public int predictionStepsPerFrame = 6;
    public Vector3 bulletVelocity;

    // Start is called before the first frame update

    void Start()
    {
        bulletVelocity = this.transform.forward * speed;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 point1 = this.transform.position;
        float stepSize = 1.0f / predictionStepsPerFrame;
        for (float step = 0; step < 1; step += stepSize)
        {
            bulletVelocity += Physics.gravity * stepSize * Time.deltaTime;
            Vector3 point2 = point1 + bulletVelocity * stepSize * Time.deltaTime;
            Ray ray = new Ray(point1, point2 - point1);
            if(Physics.Raycast(ray,(point2 - point1).magnitude))
            {
                Debug.Log("Hit");
            }

            point1 = point2;
        }

        this.transform.position = point1;
    }
    void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Vector3 point1 = this.transform.position;
        Vector3 predicteBulletVelocity = bulletVelocity;
        float stepSize = 0.01f;
        for (float step = 0; step < 1; step += stepSize)
        {
            predicteBulletVelocity += Physics.gravity * stepSize;
            Vector3 point2 = point1 + predicteBulletVelocity * stepSize;
            Gizmos.DrawLine(point1, point2);
            point1 = point2;
        }
    }
}

Here are the links to the videos if anyone cared. I also need help with sprinting and a few other things but I figured ill see how this goes.

Make a prefab for projectile. Put ProjectileScript on it. Make
[SerializeField] GameObject projectilePrefab;
in GunScript (or any gun related script) and assign prefab.
Or:
[SerializeField] ProjectileScript projectilePrefab;
but this way you will not see the prefab in selector, you’ll need to assign it by dragging from assets.
Simple variant for Shoot method (you need to make gameobject for barrel end and use it transform.position/transform.rotation):

    void Shoot()
    {
        Instantiate(projectilePrefab, transform.position, transform.rotation);
    }

You might want to put some more data into projectile, like who shooted and damage, so make a new method in ProjectileScript to set it. In this case Shoot will look like this:

    void Shoot()
    {
        ProjectileScript p = Instantiate(projectilePrefab, transform.position, transform.rotation);
        p.Set(player, speed, damage, range);
    }

If instantiating becomes too cumbersome, consider switching to an Object Pooling.

1 Like

Thanks so much, ill be sure to try this ASAP

yeah I’m still not cracking it.

Update.

So I got it working Kinda, the bullets poop out the bottom of the mag/center of the gun mesh instead of the muzzle. I think it could be because the Z BulletVelocity isn’t being saved when I hit play. It could also be my gravity settings but I can’t change them without feeling like I’m on the moon.

using UnityEngine;

public class GunScript : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField] GameObject Bullet;

public float damage = 10f;
public float range = 100f;

public Camera fpsCam;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown(“Fire1”))
{
Shoot();
}
}

void Shoot()
{
Instantiate(Bullet, transform.position, transform.rotation);
}
}

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

public class ProjectileScript : MonoBehaviour
{
[SerializeField]
public ProjectileScript sphere;
public float speed;
public int predictionStepsPerFrame = 6;
public Vector3 bulletVelocity;

// Start is called before the first frame update

void Start()
{
bulletVelocity = this.transform.forward * speed;
}

// Update is called once per frame
void Update()
{
Vector3 point1 = this.transform.position;
float stepSize = 1.0f / predictionStepsPerFrame;
for (float step = 0; step < 1; step += stepSize)
{
bulletVelocity += Physics.gravity * stepSize * Time.deltaTime;
Vector3 point2 = point1 + bulletVelocity * stepSize * Time.deltaTime;
Ray ray = new Ray(point1, point2 - point1);
if(Physics.Raycast(ray,(point2 - point1).magnitude))
{
Debug.Log(“Hit”);
}

point1 = point2;
}

this.transform.position = point1;
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Vector3 point1 = this.transform.position;
Vector3 predicteBulletVelocity = bulletVelocity;
float stepSize = 0.01f;
for (float step = 0; step < 1; step += stepSize)
{
predicteBulletVelocity += Physics.gravity * stepSize;
Vector3 point2 = point1 + predicteBulletVelocity * stepSize;
Gizmos.DrawLine(point1, point2);
point1 = point2;
}
}
}

6129569--668219--upload_2020-7-25_1-11-52.jpg

  1. Your bullet is not a prefab.
  1. As already mentioned, you need to create an empty gameobject inside the weapon in the same place where the bullet is now and use its transform when instantiating the prefab (in the example, the transform of the weapon is used, so the bullet appears in the center of the weapon).
  2. Changes to BulletVelocity in the inspector will not affect bullet flight in any way. This field shouldn’t be public at all. It would be better to make it private and update it in OnValidate when “speed” changes if you want to show trace changes in the editor.
1 Like

Hi, thanks so much for your patience I got it working for real this time I think.
I overcomplicated it but then I just went previous build and did exactly as you said.