Question by
psiDaylight · Dec 02, 2020 at 12:35 AM ·
charactercharactercontrollerjumping
Character Jumping Too Fast
The character in my 2D game jumps so fast it's as if it's teleporting. I've tried changing linear drag, gravity, etc. but nothing fixes it. The character falls at the normal speed it needs to though. I use ForceMode2D.Impulse in an attempt to mitigate it but it still basically teleports.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testCharacterController : MonoBehaviour
{
private Rigidbody2D testCharacter;
public float speed = 10f; // Public value designating character movement speed
public float jump1 = 10f; // Public value for jump height
public float jump2 = 10f; // Public value for double jump height
public bool isGrounded = false; // Checks if character is on the ground
public bool canDJump = false; // Checks if the character can perform a double jump
public bool hasDJump = false; // Checks if the character has already performed a double jump
// Start is called before the first frame update
void Start()
{
testCharacter = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float moveHorizontal = Input.GetAxisRaw("Horizontal");
testCharacter.velocity = Vector2.right * moveHorizontal * speed;
if (Input.GetKey(KeyCode.A))
{
testCharacter.velocity = new Vector2(-speed, 0f);
}
else if (Input.GetKeyUp(KeyCode.A))
{
testCharacter.velocity = new Vector2(0f, 0f);
}
if (Input.GetKey(KeyCode.D))
{
testCharacter.velocity = new Vector2(speed, 0f);
}
else if (Input.GetKeyUp(KeyCode.D))
{
testCharacter.velocity = new Vector2(0f, 0f);
}
Jump(); // Constantly calls for Jump function, and executes when jump is pressed
}
// Jump mechanic
void Jump()
{
// Checks status for if the player is mid, can double jump, or has double jumped
if (isGrounded == false)
{
canDJump = true;
if (hasDJump == true)
{
canDJump = false;
}
}
// Sets flags for can double jump and if doubled jumped to false while on ground
if (isGrounded == true)
{
hasDJump = false;
canDJump = false;
}
// Checks and performs jump when grounded
if (Input.GetKey(KeyCode.W) && isGrounded == true)
{
testCharacter.AddForce(new Vector2(0, jump1), ForceMode2D.Impulse);
}
// Checks and performs double jump when not grounded.
if (Input.GetKeyDown(KeyCode.W) && isGrounded == false && canDJump == true && hasDJump == false)
{
testCharacter.AddForce(new Vector2(0, jump2), ForceMode2D.Impulse);
hasDJump = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
jumping for different characters? 0 Answers
Changing characters 0 Answers
How to move a character in one direction only when jumping? 1 Answer
MoveTowards is curving for no reason 0 Answers
Error CS0103 0 Answers