- Home /
Force in AddForce with ForceMode.Impulse insconsistent
Sometimes my player jumps relatively high, but then he jumps relatively low. I tried several methods, but none of them worked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
public GameObject cam1;
public Transform playerBody;
RaycastHit hit;
RaycastHit hitGround;
public float MoveForce = 10f;
public float jumppadForce = 10f;
public bool hasKey;
public bool isJumping;
private void Update()
{
if(Physics.Raycast(cam1.transform.position, cam1.transform.forward, out hit, 4))
{
if (Input.GetMouseButtonDown(0))
{
switch (hit.transform.gameObject.tag)
{
case "key":
Destroy(hit.transform.gameObject);
hasKey = true;
break;
case "lock":
if (hasKey)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
break;
}
}
if (Input.GetMouseButton(0))
{
switch (hit.transform.gameObject.tag)
{
case "movableJumppad":
hit.transform.SetParent(playerBody);
break;
}
}
if (Input.GetMouseButtonUp(0))
{
switch (hit.transform.gameObject.tag)
{
case "movableJumppad":
hit.transform.SetParent(null);
break;
}
}
}
// part of jumping mechanic
if(Physics.Raycast(transform.position, Vector3.down, out hitGround, 1.2f))
{
switch (hitGround.transform.gameObject.tag)
{
case "jumppad":
isJumping = false;
break;
case "movableJumppad":
isJumping = false;
break;
}
}
else
{
isJumping = true;
}
}
// Update is called once per frame
void FixedUpdate()
{
// jumping mechanic
if (!isJumping)
{
rb.AddForce(jumppadForce * Vector3.up * Time.fixedDeltaTime, ForceMode.Impulse);
}
transform.Translate(Input.GetAxis("Vertical") * Time.deltaTime * MoveForce * Vector3.forward);
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * MoveForce * Vector3.right);
}
}
Answer by DanielGL · Feb 06 at 03:05 PM
rb.AddForce takes in cosideration the previous force of the object, so if your player was been affected by some other force before the jump, it can changes your results a bit, for jumps instead of rb.AddForce()
, try to direct set the velocity by rb.velocity = jumpForce;
it will ignore any previous velocity.
Edit: you may want to do it more like that rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
with that you can preserve your horizontal movement.
Answer by Captain_Pineapple · Feb 06 at 05:47 PM
If i ready this correctly then your issue will not be solved by @DanielGL s solution.
Your force application in itself is probably correct but you do not consider that it might be applied multiple times as your condition to apply the force (`!isJumping`) is always true if a jumppad is in range of a raycast. If for some reason your player does not leave the raycast range in one fixed update you will add the force in the next frame as well which would lead to a doubled applied force.
So to solve this you should basically disable the possibility for force application for at least a few frames to make sure that you actually left the ground.