- Home /
Player Accelerates When Walking Off Platform
So, I've had a ton of problems in Unity, but I've managed to fix them all. The last one I'm faced with goes as follows:
When I jump/ walk off a platform, my player accelerates about twice as much as he should.
I really don't understand this problem and have spent the past 6 hours trying to fix it, any help is greatly appreciated.
Here's a video: https://www.youtube.com/watch?v=bOkkQD4GQ38
Here's what my code looks like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
public CharacterController charController;
public Camera cam;
private int walkSpeed = 3;
[SerializeField] private float jumpMultiplier;
[SerializeField] private KeyCode jumpKey;
private bool isJumping;
void Awake() {
charController = GetComponent<CharacterController>();
}
void Update() {
MovePlayer();
jumpInput();
}
private void MovePlayer() {
float horiz = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) {
walkSpeed = 7;
} else {
walkSpeed = 3;
}
Vector3 moveDirSide = transform.right * horiz * walkSpeed;
Vector3 moveDirForward = transform.forward * vert * walkSpeed;
charController.SimpleMove(moveDirSide);
charController.SimpleMove(moveDirForward);
}
private void jumpInput() {
if (Input.GetKeyDown(jumpKey) && !isJumping) {
isJumping = true;
StartCoroutine(JumpEvent());
}
}
private IEnumerator JumpEvent() {
float vert = Input.GetAxis("Vertical");
Vector3 moveDirForward = transform.forward * vert * (walkSpeed / 2);
charController.SimpleMove(moveDirForward);
charController.slopeLimit = 90f;
do {
charController.Move(Vector3.up * 10 * Time.deltaTime);
yield return null;
} while (!charController.isGrounded && charController.collisionFlags != CollisionFlags.Above);
charController.slopeLimit = 45f;
isJumping = false;
}
}
Answer by Pseu · Aug 21, 2018 at 05:05 AM
Vector3 moveDirForward = transform.forward * vert * (walkSpeed / 2);
Transform.forward is in front of the object, not above it. You are also calling the same line:
charControlller.SimpleMove(moveDirForward);
twice. Once from the MovePlayer() function and once in the enumeration with slightly different values. I suspect you don't want the enumeration one unless you want the player to be forced forward when jumping? If that's the case you might want to put a if (!jumping) before your simple move in MovePlayer().
Also while the enumeration is going on this line:
charController.Move(Vector3.up * 10 * Time.deltaTime);
Is getting repeatedly called and I suspect stacking on top of each other causing the acceleration.
If none of the above is right and I've read your code horribly wrong then I suggest looking into the settings you have for the Rigidbody on your character as that can alter the way objects fall thus might be causing the acceleration bug. Try lowing your mass or turning off gravity and adding it in via code.
Tried everything including lowering mass, removing gravity, and your solutions:
Solution results:
1 Not really sure what your idea for a solution on this is
2 removed the ability to move on what ever one I removed (if i removed movedirforward, it didnt let me move forward or backward)
3 It needs to be called several times, just disables jump without it
Your answer
Follow this Question
Related Questions
Code for FP 2 Answers
How do I make a crosshair? 2 Answers
Problem With First Person View Controller 1 Answer
Standard FPS Controller - Smoothing Movement 2 Answers
FPS Controller Confusion... 0 Answers