How to throw an object towards a hit.point in first person?
Hey there guys,
I've been looking around and scratching my head for awhile and no one seems to have the solution to my particular predicament.
I'm attempting to create a first person telekinesis mechanic so that the player can throw an object directly where they are aiming.
Currently I can throw the object forward in the direction the camera is facing which is nice but the object does not travel to screen center which is where the crosshair is aiming. My potential solution is to get a raycast hit point and have the object fly towards the hit point. It sounds easy enough but I can't quite make out the logistics in code to make that happen...
Any ideas out there? Here's the bit of code I currently have to throw the object, the "else" part of the code is what I'm defaulting to while I try to come up with a simple, elegant solution to throwing an object to the center of the crosshair.
if (GrabBoi.GetComponent<Rigidbody>() != null)
{
GrabBoi.layer = LayerMask.NameToLayer("Default");
GrabBoi.GetComponent<Rigidbody>().useGravity = true;
GrabBoi.gameObject.transform.parent = null;
isGrabbed = false;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out RaycastHit hit))
{
GrabBoi.GetComponent<Rigidbody>().AddForce(transform.forward * 15f * timer, ForceMode.Impulse);
}
else
{
GrabBoi.GetComponent<Rigidbody>().AddForce(fpsCam.transform.forward * 15f * timer, ForceMode.Impulse);
}
}
Your answer
Follow this Question
Related Questions
Hello guys, why i can not add this to my code? 1 Answer
Conserve some of Rigidbody's speed when turning. 0 Answers
Moving and Rotating Rigidbody in a responsive way 2 Answers
RigidBody Controller Forces 0 Answers
Rigidbody Addforce Not Working 1 Answer