Using Velocity breaks my jumping
Hey guys I'm having a problem with my playercontroller.
For some reason when I use rb.velocity it breaks my jumping. How it breaks my jumping is by, when pressing "Jump" the player automatically goes to the maximum jump height then falls back. There seems to be a limit on the jump height unless I massively change the "Jumpforce" by a large number it will not change.
I have tried to fix this problem myself, I changed the movement to a simple WASD movement with a transform.position and the jump mechanic worked fine. Then I thought it was the MagnitudeClamp so I removed that and the outcome was the same.
public class PlayerController : MonoBehaviour {
public float newspeed = 10f;
float jumpforce = 5f;
public bool isMoving;
public bool isRunning;
//public Transform groundcheck;
private Rigidbody rb;
public LayerMask groundlayer;
public bool isGrounded;
public Vector3 jump;
void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
jump = new Vector3(0.0f, 10.0f, 0.0f);
}
private void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit))
{
if (hit.distance < 0.51f)
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
if ((Input.GetButtonDown("Jump")) && (isGrounded))
{
//Jump using the rigidbodies force of gravity.
rb.AddForce(jump * jumpforce, ForceMode.Impulse);
}
}
void FixedUpdate()
{
rb.velocity = Vector3.ClampMagnitude((rb.transform.forward * Input.GetAxis("Vertical") * newspeed) + (rb.transform.right * Input.GetAxis("Horizontal") * newspeed), newspeed);
Physics.gravity = new Vector3(0, -50f, 0);
isMoving = false;
isRunning = false;
}
Thank you for reading my thread
$$anonymous$$y eye keeps being drawn to where you directly set velocity in your FixedUpdate method. It seems like it should override whatever AddForce did, previously. While that doesn't directly create the effect you are seeing, it makes me wonder if there is some other place where you are directly setting a variable (like position, perhaps) that is causing this. $$anonymous$$aybe something in code you haven't posted?
I don't access the position anywhere else in the code, the only part that I do is in a mouse rotation script which moves the characters rotation but when disabling that to see if it affects anything it has no affect
Your answer
Follow this Question
Related Questions
Fix for inconsistent jump height? 1 Answer
Double Jump using character controller 0 Answers
Problem with Jump script 1 Answer
Jump logic issues 0 Answers
Gravity To CharacterController 0 Answers