- Home /
jumping from ledge problem
My movement script allows the player to jump 0.1s after going off a ledge so that it seems more realistical. Otherwise he would just slide off the ledge as soon as he reaches it. I have a Capsule with CharacterController.
The problem is that the player wont jump sometimes when on ground. It happens just once in 20 times but it happens and it would be irritating if it'd happen at a crucial moment. I tried the code without the ledge tolerance and it works perfectly fine but does someone have an idea how to solve the problem or does someone have an alternative?
the tolerance part would be the lines with jumpAllowTime, jumpAllowTimeTrack, waitToLand and waitToLandTrack
void Start()
{
player = GetComponent<CharacterController> ();
transform.position = spawnPoint.transform.position;
jumpSpeed = Mathf.Sqrt (2 * gravity * jumpHeight);
verticalSpeed = -gravity;
jumpAllowTimeTrack = jumpAllowTime;
movementSmoothFactor = groundSmoothFactor;
}
void Update(){
moveHorizontal = 0;
moveDepth = 0;
moveSpeed = 1.0f;
moveSpeedAir = 0.6f;
//input
onGroundInput = new Vector3 (moveHorizontal,0,moveDepth);
//normalizes the vector so that diagonal movement doesn't get too fast
if (onGroundInput.magnitude > 1) {
onGroundInput.Normalize();
}
targetGroundMovement = onGroundInput;
//lets the character walk into the direction the camera is facing
targetGroundMovement = playerCamera.transform.TransformDirection (targetGroundMovement);
targetGroundMovement.y = 0;
targetGroundMovement *= onGroundInput.magnitude;
//gravity
if (player.isGrounded) {
movementSmoothFactor = groundSmoothFactor;
jumpAllowTimeTrack = jumpAllowTime;
waitToLandTrack -= Time.deltaTime;
debugText.text = "grounded";
}
if (!player.isGrounded) {
movementSmoothFactor = airSmoothFactor;
verticalSpeed += Physics.gravity.y * Time.deltaTime;
jumpAllowTimeTrack -= Time.deltaTime;
waitToLandTrack = waitToLand;
debugText.text = "not grounded";
}
if (waitToLandTrack < 0.0f) {
verticalSpeed = -0.1f;
jumpText.text ="not jumping";
}
//jumping
if ((jumpAllowTimeTrack > 0.0f) && (Input.GetKeyDown(KeyCode.Space))) {
verticalSpeed = jumpSpeed;
jumpText.text ="jumping";
}
}
void FixedUpdate () {
/*currentMovement converges to targetGroundMovement with time
currentMovementVelocity gets processed on every function call*/
currentMovement = Vector3.SmoothDamp (currentMovement, targetGroundMovement * speed, ref currentMovementVelocity, movementSmoothFactor);
currentMovement.y = verticalSpeed;
player.Move(currentMovement * Time.deltaTime );
}
Answer by LK84 · Jan 13, 2017 at 04:45 PM
it's hard to say because there are many relevant parts of the code not provided (when and where is player.isGrounded set) but I assume the occasional non-reaction occurs due to getting Input in Update () and making the player to move/jump in the FixedUpdate(). It can occur that Update() gets called twice between two FixedUpdate() calls so your verticalSpeed value might get overwritten before FixedUpdate() is called (as far as I understand that's the value you are using to make the player jump)
you sir.......thank you very very very much :D I didnt think that the values of the variables would be necessary but youre right, it would have been better to post them too and the isGrounded attribute is from Unity's Character Controller. I put the last part of the script into the update function and now there seems to be no problem :D But isnt it recommended to use the $$anonymous$$ove() function in LateUpdate() or something? I split up those parts because the $$anonymous$$eyboard Input needs to be in Update() and the physical part of the code with collisions etc. ($$anonymous$$ove() function) needs to be in LateUpdate() or so I thought?
Yes, it's right in general to do Physic related stuff in FixedUpdate. You could use a bool, set in Update() to true and in FixedUpdate() to false and as long as the bool is true the value of the variable can't be overwritten. I converted my first comment to an answer since it helped you with your problem. I'd appreciate if you acceppt it ;)
@L$$anonymous$$84: Alright now I'm able to proceed :D Of course I'll accept!!! Thanks for your advice!!!
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How to check if object is on ground (C#)? 1 Answer
Midair movement with the character controller? 5 Answers
CharacterController Jump from moving platform. 0 Answers
Movement Jumping Help 0 Answers