- Home /
add force in ball when i kick the ball
hey friends
need some help here. i have character which i control by kinect and when i kick the ball it goes well but i want to add some force in ball but in the direction of ball is kicked
i have script but it only Add force in forward direction.
here's the script i m using.
var speed: int = 100;
function OnControllerColliderHit(hit:ControllerColliderHit){
if(hit.gameObject.tag == "ball"){
Debug.Log("We hit a object");
hit.rigidbody.AddForce(Vector3.forward * speed);
}
}
thanx cheers
Well, I should have more info before I can help you further. You should at least give me some of your code and explain how your scene is setup to get a more specific explanation.
Answer by Tim-Michels · Jul 02, 2012 at 08:38 AM
Well you should calculate a Vector3 for your direction. You should calculate it something like this:
Pseudo-code
Vector3 shootDir = ballPosition - footPosition; //calculate delta vector
shootDir.Normalize(); //normalize
ball.AddForce(shootDir * speed, ForceMode.Impulse); //Add impulse force in correct direction.
Hope this helps ;)
Cheers
@Tim $$anonymous$$ichels Thanx for reply,
i don't know how to comment under your answers bcoz of new UI of uinty answers. that's why replying here.
i m confused about how to get ball position and foot position i m noob in coding. i don't have much knowledge about scirpting.
thanx cheers
thanks for reply. it works fine.
in my project 1mass 500-2000 is to high, 90-150 is max.
@przem997 Converted your answer to a comment. Please use the Add comment link listed under each answer when posting a comment in the future. Sorry if I placed it under the wrong answer.
Answer by robertbu · Aug 17, 2013 at 02:43 PM
Try:
hit.rigidbody.AddForce(hit.moveDirection * kickPower);
For an object with a mass of 1.0, kickPower will need to be fairly large...in the range of 500 - 2000.