Search Unity

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

Discussion in 'Scripting' started by BostonGamesLLC, Aug 12, 2019.

  1. BostonGamesLLC

    BostonGamesLLC

    Joined:
    Jan 19, 2019
    Posts:
    15
    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:




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class Controls : MonoBehaviour
    7. {  
    8.     GameControls0 controls;
    9.  
    10.     public Rigidbody rb;
    11.     public Transform instantiatePos;
    12.     public GameObject canon;
    13.     public Camera cam;
    14.     public GameObject cursor;
    15.  
    16.     Vector3 canonPosition;
    17.     Vector2 moveCanon;
    18.     Vector2 rotateCanon;
    19.     Vector2 moveCrosshair;
    20.  
    21.     public float camMovSpeed = 100f;
    22.     public float canonRotateSpeed = 150f;
    23.  
    24.     //Launch metrics
    25.     public float launchSpeed = 100f;
    26.     public float xLaunchValue = 0f;
    27.     public float yLaunchValue = 0.5f;
    28.     public float zLaunchValue = 50f;
    29.      
    30.  
    31.     Vector3 target = new Vector3(0f, 0f, 0f);
    32.     Vector3 origin = new Vector3(0f, 0f, 0f);
    33.  
    34.  
    35.  
    36.  
    37.  
    38.     private void Awake()
    39.     {
    40.         //Set the new Vector to be that of the Transform you attach in the Inspector
    41.         canonPosition.Set(instantiatePos.position.x, instantiatePos.position.y, 0);
    42.  
    43.  
    44.         controls = new GameControls0();
    45.  
    46.         controls.Gameplay.Launch.performed += ctx => Launch();
    47.      
    48.         controls.Gameplay.MoveCanon.performed += ctx => moveCanon = ctx.ReadValue<Vector2>();      
    49.         controls.Gameplay.MoveCanon.canceled += ctx => moveCanon = Vector2.zero;              
    50.  
    51.         controls.Gameplay.MoveCrosshair.performed += ctx => moveCrosshair = ctx.ReadValue<Vector2>();
    52.         controls.Gameplay.MoveCrosshair.canceled += ctx => moveCrosshair = Vector2.zero;
    53.     }
    54.  
    55.     void FixedUpdate()
    56.     {
    57.         //TODO clamp values between X and Y rotations so canon and camera do not rotate too crazy when the player is aiming
    58.  
    59.      
    60.         //  adding the camMovSpeed multiplier fixed the issue where the input was not
    61.         //  registered in the Game view (even though it was in the Scene view
    62.         Vector2 m = new Vector2(-moveCanon.x, moveCanon.y) * Time.deltaTime *camMovSpeed;      
    63.         cam.transform.Translate(m, Space.World);
    64.      
    65.      
    66.         canon.transform.rotation = Quaternion.LookRotation(-cursor.transform.position, Vector3.up);
    67.  
    68.  
    69.         Vector3 c = new Vector3(-moveCrosshair.x, 0.0f, -moveCrosshair.y) * Time.deltaTime * canonRotateSpeed;
    70.         cursor.transform.Translate(c, Space.World);
    71.      
    72.     }
    73.  
    74.  
    75.     void Launch()
    76.     {
    77.         Rigidbody projectile = Instantiate(rb, transform.position, transform.rotation);
    78.         Vector3 Vo = CalculateVelocity(cursor.transform.position, transform.position, 1f);
    79.  
    80.      
    81.         projectile.velocity = Vo*Time.deltaTime*launchSpeed;
    82.  
    83.         //the below code line is not working at all, I am only able to instatiate the projectile
    84.         //projectile.AddForce(xLaunchValue, yLaunchValue, zLaunchValue*launchSpeed, ForceMode.Impulse);
    85.  
    86.  
    87.         cursor.transform.position = cursor.transform.position + Vector3.up * 0.1f;      
    88.  
    89.     }
    90.  
    91.         void OnEnable()
    92.     {
    93.         controls.Gameplay.Enable();
    94.     }
    95.  
    96.     void OnDisable()
    97.     {
    98.         controls.Gameplay.Disable();
    99.     }
    100.  
    101.  
    102.     private Vector3 CalculateVelocity(Vector3 target, Vector3 origin, float time)
    103.     {
    104.         //define the distance x and y first
    105.         Vector3 distance = target - origin;
    106.         Vector3 distanceXZ = distance;
    107.         distanceXZ.y = 0f; //this way it will only keep value of x and z component
    108.  
    109.         //create a float that represents out distance
    110.         //vertical y dist
    111.         float Sy = distance.y;
    112.         float Sxz = distanceXZ.magnitude; //dist of x z plane or length of the vector
    113.  
    114.         //calc velocity - Vars V1
    115.         float Vxz = Sxz / time;
    116.         float Vy = Sy / time + 0.5f * Mathf.Abs(Physics.gravity.y) * time;
    117.  
    118.         Vector3 result = distanceXZ.normalized; //this will return the direction of our factor
    119.         //use V1 variables to our new created vector ^
    120.         result *= Vxz;
    121.         result.y = Vy;
    122.  
    123.         return result;
    124.     }
    125. }
     
    Last edited: Aug 12, 2019
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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?
     
    BostonGamesLLC and LiterallyJeff like this.
  3. BostonGamesLLC

    BostonGamesLLC

    Joined:
    Jan 19, 2019
    Posts:
    15
    Thank you for the suggestions, I just got done checking each one and found the issue:

     
    GroZZleR likes this.