- Home /
kicking a ball with a first person controller
I am using unity trying to make a soccer game. Have a flat terrain with grass. Have a first person controller. Have a sphere (the ball). Rigidbody physics on the sphere. But, I have no idea how to make the ball move. I want it to move once I run into it with my first person controller and I have tried everything and googled it already. anyone know how to help??? view from far away http://i.imgur.com/vcd9n.png and view from close http://i.imgur.com/4TXk2.png . I need to have the first person controller (capsule) run into the ball and make it move desperately.
Answer by Piflik · Sep 26, 2011 at 07:46 PM
You can test against the distance between player and ball, and if it is below a certain threshold, you apply a force on the ball. You could additionally test for a player input.
var plChar : Transform;
var threshold: int;
var power;
if((transform.position - plChar.transform.position).magnitude < threshold) {
rigidBody.AddForce((transform.position - plChar.transform.position).normalized * power);
}
This would be a script for the ball. Pull the player into the Transform Slot via the Inspector.
Answer by robertbu · Jul 28, 2013 at 12:35 AM
The First Person Controller uses a CharacterController. To get a character controller to hit something you can use OnControllerColliderHit(). The example script on the manual page contains an example script for pushing an object. Just up pushPower for a kick. Note after you get the kick working, if you want to add some curve, see this post:
http://answers.unity3d.com/questions/377944/how-to-make-a-ball-rotate-as-per-its-moving-direct.html
Your answer
Follow this Question
Related Questions
Dragging object with a mouse from one point to another point, Have a big problem 1 Answer
Prevent physics shaking 0 Answers
Moving GUI w.r.t gameobject 0 Answers
How to Rotate and move a cube 2 Answers