- Home /
Kick a Ball - Add Force?
Hey! So I am working on this football game: http://pixelpizza.de/wordpress/?p=297
and currently i am stuck at the "kick a Ball" part. kicking works great so far and looks like this: http://postimg.org/image/z6t07aa31/
what i want do do now is, using AddForce/Impulse in a way that the ball is flying into the direction of the kick/where i kick it as soon as the feet collides with the ball when the kick animation is played or the kick button is pressed.
so what i want to know is:
how can i apply a force from the feet?
as soon as the feet collides with the ball?
if the kick is going on (not that the ball flies away every time I touch the ball)
thanks!
so, i tried it a little bit on my own and my current code looks like this:
using UnityEngine;
using System.Collections;
public class Dribble : $$anonymous$$onoBehaviour {
public float dist = 0;
public Transform target;
public Rigidbody rb;
public float forward; //150000
public float up;//2000
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
dist = Vector3.Distance(target.position, transform.position);
if (dist < 0.9f)
{
rb.AddForce((target.transform.position - transform.position).normalized * forward * Time.smoothDeltaTime);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E) && dist < 2.0f)
{
rb.AddForce(Vector3.up * up);
rb.AddForce((transform.position + target.transform.position).normalized * forward * 0.02f);
}
}
}
the code is placed on the ball, "public Transform target;" is a invisible gameobject infront of the player. in theorie it looks pretty good: http://s28.postimg.org/4hz9581nv/fb_shoot.gif
but there are some problems.
the ball is not flying in the right direction. it always moves to the same direction: http://s3.postimg.org/59k92plkh/fb_fail.gif
the ball is "wobbeling" weirdly infront of the player: http://s13.postimg.org/6kipqafs5/fb_dribble.gif
@Der_$$anonymous$$evin Since its been a long time I feel the urge to ask, have you figured out how to coordinate the animation with the "strike" ? Im trying to pull a little project myself but can´t figure how to make the strike happen only if the foot actually hit the ball (depending on the animation of course) I hope you´ve figured it out already, any advice its grateful.
PD: It really sucks that no one took the time to answer your question. I haven´t found much help myself unfortunately.
Answer by Majesteit · Jan 05, 2017 at 09:05 PM
So, I didn't really read through your code... But here's how I would do it: First you have to define a kicking state. This can be while your animation is playing, or you can start some sort of timer from when you press the key. Maybe something like this:
bool kicking = false;
float animationDuration = 1; // Animation time in seconds
/// <summary>
/// Sets the kicking state to true
/// </summary>
private void Kick()
{
if (!kicking)
{
kicking = true;
Invoke("StopKicking", animationDuration);
}
}
/// <summary>
/// Sets the kicking state to false
/// </summary>
private void StopKicking()
{
kicking = false;
}
Now, from the ball you can check collision using the OnCollisionEnter() method. When there's collision with the foot, and kicking is true, apply a force. Something like this:
private float kickForce = 10;
private void OnCollisionEnter(Collision other)
{
if (other.transform.name == "Player" && other.transform.GetComponent<PlayerBehaviour>().kicking)
{
Vector3 direction = (other.transform.position - transform.position).normalized;
rigidbody.AddForce(-direction * kickForce, ForceMode.Impulse);
}
}
I haven't tested any of this, but I hope you figure it out using this.
Your answer
Follow this Question
Related Questions
Ball physics help 2 Answers
AddForce made a ball fall with no accelleration 1 Answer
Speed Cap for Rolling Ball 1 Answer
Realistic player movement? 2 Answers
Collision going through collider 0 Answers