Trying to throw an item to my crosshair direction via ray

Hey… I am trying with this more than 5 days now…
I am trying to make a simple mechanic that is allowing me to throw my items at the center of my screen where my reticle (crosshair) is located… I am using cinemachine, and even tried to simplify that mechanic only when my second “aim” camera is activated…
But it still does not wanna throw the item exactly on the crosshair… I tried making raycast hit function but it’s not working. The item just flow in the direction that is not the crosshair aim at all… (Maybe it flows in direction of the camera, but not of crosshair direction)
Here is my code that i’ve written until now for raycasting and throwing function:

if (ObjectIwantToPickUp)

            {
                Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * 100, Color.red);
                RaycastHit hit;
                Vector3 hitPos;
                if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit))
                {
                    hitPos = hit.point;
                }
                else
                {
                    hitPos = Camera.main.transform.position + Camera.main.transform.forward * 99;
                }

                 forceDirection = hitPos - ObjectIwantToPickUp.transform.position;
            }

And here is for throwing:

void throwObject()
        {
            if (hasItem == true && Input.GetKeyDown("t"))
            {
                BoxCollider[] bc = ObjectIwantToPickUp.GetComponents<BoxCollider>();
                foreach (BoxCollider b in bc)
                {
                    b.enabled = true;
                }
                ObjectIwantToPickUp.GetComponent<BoxCollider>().enabled = true;
                ObjectIwantToPickUp.GetComponent<Rigidbody>().isKinematic = false; // make the rigidbody work again
                //ObjectIwantToPickUp.GetComponent<Rigidbody>().useGravity = false; // make the rigidbody work again
                hasItem = false;

                ObjectIwantToPickUp.transform.parent = null; // make the object no be a child of the hands
                ObjectIwantToPickUp.GetComponent<Rigidbody>().AddForce(forceDirection.normalized * 15, ForceMode.Impulse);
                ObjectIwantToPickUp.GetComponent<Rigidbody>().AddTorque(ObjectIwantToPickUp.transform.TransformDirection(Vector3.right) * 100, ForceMode.Impulse);
              
            }

Here is the full script in case I missed something: hatebin
Please, give me a hint, where am I wrong.
I am using cinemachine with two cameras and one Main camera with braincomponent in it.
Also, I am using a define position script, with simple two Vector3 positions in it.
I use that because I needed the picked up item to be parented exactly on pre-defined position in my hand

I have two cameras: Free Look camera and Virtual Camera (for aiming purposes) I change them via this script for changing the priority :

https://hatebin.com/chbofizvuk

Here is a short video what is not working:

https://vimeo.com/499738119

As u see, the “stick” in the scene does not match the crosshair when it’s throwed.

Excuse me if I wasn’t clear enough and for the messy code :smile:

I recommend making a fresh stripped-down scene so you can get some hard numbers out of what is going on.

Liberally sprinkle Debug.Log() statements through your code to display information in realtime.

The values you want are the source data going into the computations, and the resultant forces.

Face your character in one direction and throw. Face your character in another direction and throw.

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.

Hey! Thank for you speed response… Actually all my code is working, but not in the desired way :smile:
If I did not “aim” and use my freelook camera the code is working and my item is throwing in the direction that my player is facing…
I tried debugging most of the properties and functions but did not found the reason behind this behavior.
I am pretty new to all this and maybe it is just beyond my capabilities… I tried making this without raycasting, but with no success too.
The cinemachine cameras are the thing that confuses me (and i think there is my mistakes), but i’ve chosen them because they were fast and easy solution for my problems with less to none code.

Once again, thank you for your answer, but I am not sure that I will be able to handle this on my own.

Your using the Camer.main method, is there more than one camera in your scene? You mentioned Cinamachine, and different views, so I’m curious. As Camera.main will only target the main camera by the object tag.

Hello all. Thanks for the suggestions. With a little help, I was able to instantiate an VirtualCameraBase in my public variables with this

[SerializeField] Cinemachine.CinemachineVirtualCameraBase aimCam;

After that, I was able to take the position and rotation (I guess) with this

Vector3 aimOrigin = aimCam.State.FinalPosition;
                Vector3 aimDir = aimCam.State.FinalOrientation * Vector3.forward;

And finally I took the forceDirection and apply it

ObjectIwantToPickUp.GetComponent<Rigidbody>().AddForce(forceDirection.normalized * 30f + Vector3.up * 5f, ForceMode.Impulse);

I don’t know if that is the cheapest and clear solution, but for my learning purposes it’s good. I will try to refactor and clear the code later.
Thanks for helping me. :slight_smile:

2 Likes