Answer the question
In order to leave comments, you need to log in
How to fix the jump error in this code?
Hello, please help me with this error. Since I have little experience with sishke, I have no idea why this error occurs. Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[SerializeField]
private float moveSpeed = 1f;
[SerializeField]
private float jumpForce = 10f;
[SerializeField]
private float fallMultiplier = 2.5f;
[SerializeField]
private float lowJumpMultiplier = 2f;
private Rigidbody rb;
private bool doJump = false;
private bool isJumping = false;
void Awake()
{
rb = transform.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update() {
if(Input.GetButtonDown("Jump") && !isJumping) {
doJump = true;
isJumping = true;
}
}
// FixetUpdate is called once per fixed Timestep (50 times/second default)
void FixedUpdate()
{
Vector3 moveVect = Vector3.forward;
moveVect = moveVect.normalized * moveSpeed * Time.deltatime;
rb.MovePosition(transform.position + moveVect);
if(doJump) {
rb.AddForce(Vector3.up * jumpforce, ForceMode.Impulse);
doJump = false;
}
if(rb.velocity.y < 0) {
// multiply fall gravity
rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltatime;
} else if(rb.velocity.y > 0 && !Input.GetButton("Jump")) {
rb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltatime;
}
}
void OnCollisionEnter(Collision other) {
if(other.gameObject.CompareTag("Ground"))
isJumping = false;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question