- Home /
[throwing an projectile to the nearest target]
function pass(receiver:Transform){
var targetDir = receiver.position - transform.position;
if (hunterBall.rigidbody.GetComponent("BallBehavior").myOwner == transform ){
if( Vector3.Angle (transform.forward, targetDir) <130 ){
hunterBall.rigidbody.GetComponent("BallBehavior").rigidbody.velocity = targetDir.normalized*passSpeed;
hunterBall.rigidbody.GetComponent("BallBehavior").myOwner = null;
}
else hunterBall.rigidbody.GetComponent("BallBehavior").rigidbody.velocity = transform.TransformDirection(Vector3(0,0,passSpeed));
hunterBall.SendMessage ("Thrown");
hunterBall.rigidbody.GetComponent("BallBehavior").myOwner = null;
}
}
So I have this function to pass an projectile from one player to another, the problem here is that I want the projectile to go to the nearest player at an certain distance from it, and not just the one I set on the Transform as a receiver, any thoughts on how I can do that? perhaps set a RayCast and ad force to the projectile directing to whichever player is the closest? would that work? how should I proceed? Thanks everyone.
Answer by fafase · Jan 15, 2015 at 06:42 AM
Look at the "Another example":
http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
if you know ahead the other objects, place them all in an array right from the Start and only use the algorithm without FindGameObjectWithTag
Thoughts are that you are using the wrong method to throw your ball. Once you have the closest one, you need to take the distance and figure out the force based on the angle and the distance. Best choice would be to keep a 45 degree angle so that it makes it a little more simple. Finding the force can be a pain since it will depend on the geometry and the various settings for the friction and so on.
But you don't seem to use the method as it looks. First find the closest then use its position to throw. You seem to be trying to throw in the middle of the process.
Alright, thanks for the input, I'm gonna work more into it.