Third-Person Throwing

Can’t seem to make this work…

What I’m trying to accomplish is the ability for the player (model) to throw a ball at whatever object in the world they click on. I’m not asking anyone to GIVE me the scripts (however, example scripts are GREAT!). I like to do my own work…but I just can’t seem to figure this out.

  • Player clicks somewhere on the screen.
  • Scripting determines if there’s an object within a certain distance from the player.
  • If there IS…
  • Have the player first face the object. Then throw the ball at that object. (Obviously will require some math to figure out how much force to apply to the ball to make it reach the target. This is an area where I need the most help.)
  • If there IS NOT, throw the ball in the direction the player is facing.

I know this is going to require ray casting, but no matter how much I try, I can’t get the ray cast ScreenPointToRay to work with my game. I’m thinking it’s because I’m using a third-person and not a first-person. I have the mechanics in place already to have the ball instantiate in the player’s hand, the animation of the player throwing the ball, and the mechanics which tells the game to release the ball from the player’s hand once it’s at its apex. So I’m just in need of help to figure out how to make the ball’s trajectory, the place where the player clicked.

Thanks in advance for all help!

Hi! I will try to help you through this as best I can, as I am still quite new to scripting. I am giving you one way to do it, the easier way, but any object in range will be targeted. Make a Collider Component in your player. If you have one to keep on the ground, make another. Make sure it DOES NOT touch the ground. Select Is Trigger in the components properties. Change its size to match the range you want. Here is some simple code you can modify.

#pragma strict

function OnTriggerEnter(Collider Other){
     Debug.Log("Object in range!"):
}
function OnTriggerExit(Collider Other){
     Debug.Log("No Object");
}

All you have to change is what is in the functions. Good luck!
If you need further help don’t hesitate to ask!
:slight_smile:

Thanks. I never thought of using a collider to dictate the range. Good idea. Now the only thing I’m still stumped on is the darn ScreenPointToRay.