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.
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
1 Like
Well, I’ve got something that works, but I’m not certain the math is right:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestKick : MonoBehaviour
{
public float force;
public float distance;
public Transform camera;
NewPlayerMovement playerMovement;
int layerMask;
RaycastHit hit;
Rigidbody playerRB;
// Start is called before the first frame update
void Start()
{
layerMask = LayerMask.GetMask("Enemy Movement") | LayerMask.GetMask("Default");
playerRB = GetComponent<Rigidbody>();
playerMovement = GetComponent<NewPlayerMovement>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("e")){
Debug.Log("Kicking");
if (Physics.Raycast(camera.position, transform.TransformDirection(Vector3.forward), out hit, distance, layerMask)) { Debug.Log("Kicked");
Rigidbody hitRB = hit.transform.gameObject.GetComponent<Rigidbody>();
if (hitRB == null){
Debug.Log("Does not have RB");
playerMovement.isGrounded = false;
playerMovement.isFalling = false;
playerRB.AddForce(-camera.transform.forward * force);
} else {
Debug.Log("Has RB");
float playerForce = 1;
float hitForce = 1;
if (playerRB.mass < hitRB.mass)
{
hitForce = (playerRB.mass * playerRB.mass) / (hitRB.mass * hitRB.mass);
playerForce = 1 - hitForce;
}
if (playerRB.mass > hitRB.mass)
{
playerForce = (hitRB.mass * hitRB.mass) / (playerRB.mass * playerRB.mass);
hitForce = 1 - playerForce;
}
if (playerRB.mass == hitRB.mass)
{
playerForce = 1;
hitForce = 1;
}
playerRB.AddForce(-camera.transform.forward * force * playerForce);
hitRB.AddForceAtPosition(camera.transform.forward * force, hit.point);
Debug.Log("Player Force = " + playerForce);
Debug.Log("Hit Force = " + hitForce);
}
}
}
}
}
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.
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?
1 Like
Maybe.
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
1 Like