I am trying with this more than 5 days now...
I am trying to make a simple mechanic in third person mode 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:
Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * 100, Color.red);
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit))
hitPos = hit.point;
hitPos = Camera.main.transform.position + Camera.main.transform.forward * 99;
forceDirection = hitPos - ObjectIwantToPickUp.transform.position;
And here is for throwing:
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: https://hatebin.com/uppepjkemf
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 :
Here is a short video what is not working:
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 :D