Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Kicking

Discussion in 'Physics' started by Not_Sure, Nov 18, 2022.

  1. Not_Sure


    Dec 13, 2011
    I'm wanting to add a kick to my game in which the player can kick other physics objects, like barrels and enemies, and have the object move from the player in the direction they look and the player move backwards from where they look.

    Pretty simple, but I really want the two applied forces to be relative to their weight, AND I want to be able to push away 100% if it is a non-physics object, AND I want the player's current forward velocity to add to the impact.

    Basically I want the it to double as a wall jump.

    Obviously the first step is to check if the object has a rigidbody, if it doesn't just apply a static force in the opposite direction, regardless of the player velocity.

    But what if it does have one?

    Is there a way I can do this without a bunch of math?

    If not, anyone have a link to some useful math?

    EDIT: also, is there a way to apply force relative to a point on a collider? If I just apply force to the rigidbody of, say a crate, it is only going to apply that force to the center of mass. So currently if I kick its corner it will not spin it just rolls over.
    Last edited: Nov 18, 2022


    Feb 8, 2022
    hey guy watch out I have this exact feature in my game and I'll hunt you down if you figure it out

    what I did for calculating the explosion to more consistently move things was by triggering an explosion through the contact at a point i determine between the object and player by lerping.

    to wall jump you just apply a force to the player in the reverse direction of the wall or object, of course you must have a rigidbody based controller to apply an explosion force
    unity will do the work for you involving weights and whatnot depending on the explosion type; i tend to prefer impulse

    hope this can help you get started let me know if you need to know anything else
    Not_Sure likes this.
  3. Not_Sure


    Dec 13, 2011
    Well, I've got something that works, but I'm not certain the math is right:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class TestKick : MonoBehaviour
    6. {
    7.     public float force;
    8.     public float distance;
    9.     public Transform camera;
    10.     NewPlayerMovement playerMovement;
    11.     int layerMask;
    12.     RaycastHit hit;
    13.     Rigidbody playerRB;
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         layerMask = LayerMask.GetMask("Enemy Movement") | LayerMask.GetMask("Default");
    18.         playerRB = GetComponent<Rigidbody>();
    19.         playerMovement = GetComponent<NewPlayerMovement>();
    20.     }
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Input.GetKeyDown("e")){
    26.             Debug.Log("Kicking");
    27.             if (Physics.Raycast(camera.position, transform.TransformDirection(Vector3.forward), out hit, distance, layerMask)) { Debug.Log("Kicked");
    28.                 Rigidbody hitRB = hit.transform.gameObject.GetComponent<Rigidbody>();
    29.                 if (hitRB == null){
    30.                     Debug.Log("Does not have RB");
    31.                     playerMovement.isGrounded = false;
    32.                     playerMovement.isFalling = false;
    33.                     playerRB.AddForce(-camera.transform.forward * force);
    34.                 } else {
    35.                     Debug.Log("Has RB");
    36.                     float playerForce = 1;
    37.                     float hitForce = 1;
    38.                     if (playerRB.mass < hitRB.mass)
    39.                     {
    40.                         hitForce = (playerRB.mass * playerRB.mass) / (hitRB.mass * hitRB.mass);
    41.                         playerForce = 1 - hitForce;
    42.                     }
    43.                     if (playerRB.mass > hitRB.mass)
    44.                     {
    45.                         playerForce = (hitRB.mass * hitRB.mass) / (playerRB.mass * playerRB.mass);
    46.                         hitForce = 1 - playerForce;
    47.                     }
    48.                     if (playerRB.mass == hitRB.mass)
    49.                     {
    50.                         playerForce = 1;
    51.                         hitForce = 1;
    52.                     }
    53.                     playerRB.AddForce(-camera.transform.forward * force * playerForce);
    54.                     hitRB.AddForceAtPosition(camera.transform.forward * force, hit.point);
    55.                     Debug.Log("Player Force = " + playerForce);
    56.                     Debug.Log("Hit Force = " + hitForce);
    57.                 }
    58.             }
    59.         }
    60.     }
    61. }
    It looks good enough, but I still need to clamp the angle of the kick so that I'm not kicking upward. Also, the wall jumping is freaking awesome.

    Also, I'm needing to add some special instances to do a drop kick when in the air and to add the velocity of the player to the kick.


    Feb 8, 2022
    I have a dropkick too. the way i do all my math is based off of the approach used in postal 2 where i multiply a set explosion force by a value determined by whether the player is in the air, standing, or crouching. I also multiply by player speed so as to make it so you can't walljump with low momentum, but be careful and clamp this cuz it gets big quickly

    Maybe you could limit the angle through local eulers with inverse transform and clamping x y z individually?
    Not_Sure likes this.
  5. Not_Sure


    Dec 13, 2011

    I did try out the code above without clamping it and was hitting some crazy high speeds, but considering the player is already going half the speed of sound (no, really, I did the math) I’m okay with that.

    I actually want to add an Easter egg where if they can manage to go faster than the speed of sound it creates a sonic boom. LOL
    POOKSHANK likes this.