Projectile instantiating but just falls, my velocity scripting has no effect

I am using Unity 2019.2.0 with the LWRP and the new input system. Everything else works correctly except the projectile just falls to the ground instead of launching off. The velocity/addforce scripts I have attempted seem to be ignored and I am not sure why.

  • there is a Rigidbody on the projectile and is not set to kinematic
  • the projectile variable is initialized as a Rigidbody
  • there are no colliders in the way at the instantiate point
  • there are no accompanying console errors

I have attached a video of the issue and the script below:

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

public class Controls : MonoBehaviour
{  
    GameControls0 controls;

    public Rigidbody rb;
    public Transform instantiatePos;
    public GameObject canon;
    public Camera cam;
    public GameObject cursor;

    Vector3 canonPosition;
    Vector2 moveCanon;
    Vector2 rotateCanon;
    Vector2 moveCrosshair;

    public float camMovSpeed = 100f;
    public float canonRotateSpeed = 150f;

    //Launch metrics
    public float launchSpeed = 100f;
    public float xLaunchValue = 0f;
    public float yLaunchValue = 0.5f;
    public float zLaunchValue = 50f;
     
  
    Vector3 target = new Vector3(0f, 0f, 0f);
    Vector3 origin = new Vector3(0f, 0f, 0f);





    private void Awake()
    {
        //Set the new Vector to be that of the Transform you attach in the Inspector
        canonPosition.Set(instantiatePos.position.x, instantiatePos.position.y, 0);


        controls = new GameControls0();

        controls.Gameplay.Launch.performed += ctx => Launch();
      
        controls.Gameplay.MoveCanon.performed += ctx => moveCanon = ctx.ReadValue<Vector2>();      
        controls.Gameplay.MoveCanon.canceled += ctx => moveCanon = Vector2.zero;              

        controls.Gameplay.MoveCrosshair.performed += ctx => moveCrosshair = ctx.ReadValue<Vector2>();
        controls.Gameplay.MoveCrosshair.canceled += ctx => moveCrosshair = Vector2.zero;
    }

    void FixedUpdate()
    {
        //TODO clamp values between X and Y rotations so canon and camera do not rotate too crazy when the player is aiming

      
        //  adding the camMovSpeed multiplier fixed the issue where the input was not
        //  registered in the Game view (even though it was in the Scene view
        Vector2 m = new Vector2(-moveCanon.x, moveCanon.y) * Time.deltaTime *camMovSpeed;      
        cam.transform.Translate(m, Space.World);
      
      
        canon.transform.rotation = Quaternion.LookRotation(-cursor.transform.position, Vector3.up);


        Vector3 c = new Vector3(-moveCrosshair.x, 0.0f, -moveCrosshair.y) * Time.deltaTime * canonRotateSpeed;
        cursor.transform.Translate(c, Space.World);
      
    }


    void Launch()
    {
        Rigidbody projectile = Instantiate(rb, transform.position, transform.rotation);
        Vector3 Vo = CalculateVelocity(cursor.transform.position, transform.position, 1f);

      
        projectile.velocity = Vo*Time.deltaTime*launchSpeed;

        //the below code line is not working at all, I am only able to instatiate the projectile
        //projectile.AddForce(xLaunchValue, yLaunchValue, zLaunchValue*launchSpeed, ForceMode.Impulse);


        cursor.transform.position = cursor.transform.position + Vector3.up * 0.1f;      

    }

        void OnEnable()
    {
        controls.Gameplay.Enable();
    }

    void OnDisable()
    {
        controls.Gameplay.Disable();
    }
  

    private Vector3 CalculateVelocity(Vector3 target, Vector3 origin, float time)
    {
        //define the distance x and y first
        Vector3 distance = target - origin;
        Vector3 distanceXZ = distance;
        distanceXZ.y = 0f; //this way it will only keep value of x and z component

        //create a float that represents out distance
        //vertical y dist
        float Sy = distance.y;
        float Sxz = distanceXZ.magnitude; //dist of x z plane or length of the vector

        //calc velocity - Vars V1
        float Vxz = Sxz / time;
        float Vy = Sy / time + 0.5f * Mathf.Abs(Physics.gravity.y) * time;

        Vector3 result = distanceXZ.normalized; //this will return the direction of our factor
        //use V1 variables to our new created vector ^
        result *= Vxz;
        result.y = Vy;

        return result;
    }
}

Well check the obvious issues first:

  • Does Vo / CalculateVelocity() return a valid value?
  • Is launchSpeed set to 100 in the inspector still (maybe it serialized at 0 and you changed the default value later)?
  • Is something else setting the velocity on the projectile?
2 Likes

Thank you for the suggestions, I just got done checking each one and found the issue:

1 Like