- Home /
Please help me with the juuuuumppppp
I am making an fps and have basic movement in - besides the jumping. I browsed stack and unity forums for hours and nothing works.
My code looks like this:
public class PlayerController: MonoBehaviour
{
[SerializeField] Transform playerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] bool lockCursor = true;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField][Range(0.0f,0.5f)] float moveSmoothTime = 0.3f;
[SerializeField][Range(0.0f,0.5f)] float mouseSmoothTime = 0.03f;
[SerializeField] float gravity = -13.0f;
float cameraPitch = 0.0f;
float velocityY = 0.0f;
CharacterController con = null;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
public Rigidbody rb;
public float jumpSpeed = 1.0f;
// private float gravity = 14.0f;
void Start()
{
// rb.velocity += jumpSpeed * Vector3.up;
con = GetComponent<CharacterController>();
if (lockCursor){
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
// Update is called once per frame
void Update()
{
UpdateMouseLook();
UpdateMovement();
}
void FixedUpdate()
{
if(con.isGrounded){
Debug.Log("grounded");
if(Input.GetKey(KeyCode.Space)){
Debug.Log("WOOOOOOOOOOOOOOOOOOOORK");
rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
}
else{
}
}
else{
Debug.Log("airtime boiiiiii");
// gravity = -13;
}
}
void UpdateMouseLook(){
Vector2 targerMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targerMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
void UpdateMovement(){
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (con.isGrounded){
velocityY = 0.0f;
}
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;
con.Move(velocity * Time.deltaTime);
}
}
Any help would be appreciated! Thanks in advance.
Answer by DenisIsDenis · May 06, 2021 at 08:31 AM
Hello. Try using
rb.AddForce(rb.transform.up * jumpSpeed);
instead
rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
on line 43
Answer by ncp12 · May 06, 2021 at 11:43 PM
Hi,
Thanks for trying, but it doesn't work. It detects the space bar being pressed, but I don't move up. Is there anything else I could try?
Thanks!
Check if there is a check mark opposite from isKinematics in the Rigidbody component. It should be disabled.
I checked the script in the Unity and fixed the errors. Firstly, the Rigidbody component is unnecessary. Second, the code
if (con.isGrounded){
velocityY = 0.0f;
}
stops the up and down movement. Thirdly, when you press the spacebar, you need to change the velocityY variable:
velocityY = jumpSpeed * 10;
Answer by ElmisteriosoYtz · May 09, 2021 at 04:06 AM
Hello friend try to use this
rb.velocity = Vector2.Up * jumpSpeed;
in place of this
rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
if this rb.velocity = Vector2.Up * jumpSpeed trow you a error use this
rb.velocity = Vector2.Up * jumpSpeed * TIme.deltaTIme;
remember "The truth is out there"
Answer by FrozenTrident · May 09, 2021 at 04:46 AM
ok from what I can see your jump speed is on 1 so it can very likely be that my second guess is that there is something wrong with`rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);` i always use forcemode.inpulse
Your answer
Follow this Question
Related Questions
unity 5 first person controller jump problem 2 Answers
How to force a player to jump? 2 Answers
Custom Character Script Allowing Movement in the Air? 0 Answers
How can I set a timer? 1 Answer
Jumping away from a wall 1 Answer