Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Der_Kevin · Jan 05, 2016 at 03:54 AM · physicsaddforceball

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Der_Kevin · Jan 04, 2016 at 02:27 PM 0
Share

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.

  1. the ball is not flying in the right direction. it always moves to the same direction: http://s3.postimg.org/59k92plkh/fb_fail.gif

  2. the ball is "wobbeling" weirdly infront of the player: http://s13.postimg.org/6kipqafs5/fb_dribble.gif

avatar image DaviLope · Jan 05, 2017 at 06:04 PM 0
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moritzcerst · Jan 27, 2021 at 10:43 AM 0
Share

Works perfectly. Thank You!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

45 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ball physics help 2 Answers

AddForce made a ball fall with no accelleration 1 Answer

Speed Cap for Rolling Ball 1 Answer

add force to object that has 2 different rigid bodies 0 Answers

The Physics of Ball Spin in Tennis, Topspin and Backspin 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges