- Home /
How do I make a character Lunge?
I want to make my character do a long jump (Think half life 1), while I thought i could do it simply by adding an X value to my Jumping addForce, I end up with my character going in a straight line, up a little, and then stopping
void Leap()
{
if (maxJumps > 0)
{
maxJumps = maxJumps - 1;
Vector3 v = playerBody.velocity;
v.y = 0f;
playerBody.velocity = v;
if (right == true)
{ LeapFor = leapForce; }
else if (right == false) { LeapFor = leapForce * -1f; }
playerBody.AddForce(new Vector2(LeapFor, jumpForce), ForceMode2D.Impulse);
isOnGround = false;
}
if (maxJumps == 0)
{
return;
}
}
jump.png
(56.0 kB)
Comment
Answer by Dragovick · Nov 15, 2019 at 02:53 AM
Temp Answer if this isn't resolved better later, this is by no means an actual solution, but it's a bandaid fix
So far your best bet is to suspend your movement until the jump is complete, (possibly find a way to have both movement and addforce separate?) I managed this by using the isOnGround bool
if (isOnGround == true)
{
Vector3 moveVelocity = new Vector3(Input.GetAxis("moveX") * (playerSpeed * 50.0f) * Time.fixedDeltaTime, playerBody.velocity.y, 0.0f);
playerBody.velocity = moveVelocity;
}
void Leap()
{
if (maxJumps > 0)
{
isJumping = true;
Debug.Log("BOUNDS");
maxJumps = maxJumps - 1;
Vector3 v = playerBody.velocity;
v.y = 0f;
v.x = 0f;
playerBody.velocity = v;
if (right == true)
{ LeapFor = leapForce; }
else
if (right == false) {LeapFor = leapForce * -1f; }
playerBody.AddForce(new Vector2(LeapFor, jumpForce), ForceMode2D.Impulse);
isOnGround = false;
}
if (maxJumps == 0)
{
return;
}
}
Answer by Cornelis-de-Jager · Nov 15, 2019 at 03:24 AM
I am going to assume you have a rigid body.
get a new jump angle. Lets say 45* to keep it easy.
RigidBody rb;
void Start () {
rb = GetComponent<RigidBody>();
}
void Jump() {
var dir = new Vector2 (1, 1);
var power = 5;
rb.AddForce(dir * power, ForceMode.Impulse);
}