Trying to shoot a projectile but it doesn't move, help!

ok so, I have a simple project with a player and a staff held by the player. I want to shoot projectiles from the staff to the middle of the screen. This is my script:

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

public class Staff : MonoBehaviour
{

    public Camera fpsCam;
    public GameObject proj;
    public Transform spawnPoint;
    private Vector3 destination;
    public float projspeed;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1")){

            Shoot();
        }


    }

    void Shoot ()
    {
      
        Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit))
            destination = hit.point;
        else
           destination = ray.GetPoint(1000);
      
        Instantiate(proj, spawnPoint.position, Quaternion.identity);
        proj.GetComponent<Rigidbody>().velocity = (destination - spawnPoint.position) * projspeed;
    }




}

It gives no errors or anything, just doesn’t move the projectile. How do I fix it?

EDIT: Resolved! New code for anyone googling this below:

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

public class Staff : MonoBehaviour
{

    public Camera fpsCam;
    public GameObject proj;
    public Transform spawnPoint;
    private Vector3 destination;
    public Rigidbody rb;
    public float projSpeed;

    // Update is called once per frame
    void Start()
    {


    }
    void Update()
    {
        if (Input.GetButtonDown("Fire1")){

            Shoot();
        }


    }

    void Shoot ()
    {
       
        Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit))
            destination = hit.point;
        else
           destination = ray.GetPoint(1000);

        instantiateProj();

       
        rb.velocity = Camera.main.transform.forward * 40;
        //proj.GetComponent<Rigidbody>().velocity = (destination - spawnPoint.position) * projspeed;
        //Debug.Log(ProjRb.velocity);
    }
    void instantiateProj()
    {
        var projObj = Instantiate(proj, spawnPoint.position, Quaternion.identity) as GameObject;
        projObj.GetComponent<Rigidbody>().velocity = (destination - spawnPoint.position).normalized * projSpeed;

    }
}

What you put there certainly seems reasonable, assuming there is a Rigidbody and all. Time to start inspecting values. Break line 37 apart into the individual calculations and see if you’re even setting the velocity to something reasonable.

In general, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

If you are running a mobile device you can also see the console output. Google for how.

Thanks a lot for the help :slight_smile: I ran some Debug.Log statements and it seems the velocity isn’t the problem, but rather setting the velocity of the projectile’s rigidbody to the desired value. I did some more testing trying to set the velocity to a number rather than a variable but it still stayed in place (also it’s not the projectile as when spawning 2 projectiles on top of each other they collide and fly away). below is the new code, no idea what i’m doing wrong.

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

public class Staff : MonoBehaviour
{

    public Camera fpsCam;
    public GameObject proj;
    public Transform spawnPoint;
    private Vector3 destination;
    public Rigidbody rb;
    public float projspeed;

    // Update is called once per frame
    void Start()
    {
        rb = proj.GetComponent<Rigidbody>();

    }
    void Update()
    {
        if (Input.GetButtonDown("Fire1")){

            Shoot();
        }


    }

    void Shoot ()
    {
       
        Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit))
            destination = hit.point;
        else
           destination = ray.GetPoint(1000);
       
        Instantiate(proj, spawnPoint.position, Quaternion.identity);
        rb.velocity = new Vector3 (1,1,1);
        //proj.GetComponent<Rigidbody>().velocity = (destination - spawnPoint.position) * projspeed;
        //Debug.Log(ProjRb.velocity);
    }




}

Go though the usual check list: Make a fresh object, put an RB on it, make sure it is NOT kinematic, make sure it has no other scripts controlling its velocity, make sure when you spawn it it’s collider is not “Wedged” into something else like your body, etc.

Debug, debug, debug

Solved it, just needed to change some code apparently