Search code examples
c#unity-game-enginegame-physics

Code to throw object doesn't gain any height


I am trying to instantiate and throw objects into the air on mouse clicks. The objects will spawn as expected but do not gain any height on the throw.

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

public class shooter : MonoBehaviour
{
public GameObject powercell; //link to the powerCell prefab
public GameObject Dynamite; // same as above but for dyno
public int no_cell; //number of powerCell owned
public int no_Dynamite; // same as above but for dyno
public AudioClip throwSound; //throw sound
public float throwSpeed = 20;//throw speed


void Start()
{
    no_Dynamite = 0; // no dynos on spawn
    no_cell = 10000; // one cell on spawn
}

public void Update()
{
    //if left control (fire1) pressed, and we still have at least 1 cell
    if (Input.GetButtonDown("Fire1") && no_cell > 0)
    {
        no_cell--; //reduce the cell
                   //play throw sound
        AudioSource.PlayClipAtPoint(throwSound, transform.position);
        //instantaite the power cel as game object
        GameObject cell = Instantiate(powercell, transform.position,
        transform.rotation) as GameObject;
        //ask physics engine to ignore collison between
        //power cell and our FPSControler
        Physics.IgnoreCollision(transform.root.GetComponent<Collider>(),
        cell.GetComponent<Collider>(), true);
        //give the powerCell a velocity so that it moves forward
        cell.GetComponent<Rigidbody>().velocity = transform.forward * throwSpeed;
    }


    if (Input.GetButtonDown("Fire2") && no_Dynamite > 0)
    {
        no_Dynamite--; //reduce the cell
                       //play throw sound
        AudioSource.PlayClipAtPoint(throwSound, transform.position);
        //instantaite the power cel as game object
        GameObject Dyn = Instantiate(Dynamite, transform.position,
        transform.rotation) as GameObject;
        //ask physics engine to ignore collison between
        //power cell and our FPSControler
        Physics.IgnoreCollision(transform.root.GetComponent<Collider>(),
        Dyn.GetComponent<Collider>(), true);
        //give the powerCell a velocity so that it moves forward
        Dyn.GetComponent<Rigidbody>().velocity = transform.forward * throwSpeed;
    }
}

//
void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Bomb") // give us cells when we pick up the collectable
    {
        no_Dynamite = 3; ; //increment that boi
        Destroy(other.gameObject);
    }

}
}

Solution

  • You need to add an upwards component to the thrown object's velocity. If you know how much upwards you want on a scale of 0 to 1, you can use Vector3.Slerp to figure out the direction you need.

    // give the dynamite a velocity so that it moves up + forward
    Rigidbody dynamiteRB = Dyn.GetComponent<Rigidbody>();
    
    float upness = 0.5f; // 0f = horizontal ~ 1f = vertical
    Vector3 throwDirection = Vector3.Slerp(
            transform.forward, 
            Vector3.up, 
            upness
            );
    
    dynamiteRB.velocity = throwDirection * throwSpeed;
    

    If you want to throw based on the camera, but want to adjust the angle from what the camera gives you, you can base the direction off of Camera.main.transform instead:

    // give the dynamite a velocity so that it moves up + forward
    Rigidbody dynamiteRB = Dyn.GetComponent<Rigidbody>();
    
    // 0f   = aim exact direction the camera is pointing
    // 0.2f = aim slightly higher than camera is pointing
    // 1f   = aim directly up
    float additionalUpness = 0.0f; 
    Vector3 throwDirection = Vector3.Slerp(
            Camera.main.transform.forward, 
            Vector3.up, 
            additionalUpness 
            );
    
    dynamiteRB.velocity = throwDirection * throwSpeed;