- Home /
2D Platformer Bug
I have a project where I have a green ball that has Circle Collider2D, Rigidbody2D, and charatercontrol script. The charatercontrol script handles movement,jumping and dashing.I learned that there are some tricks that platformer games use such as being able to jump if you have left the ground (for example:)0.2 seconds ago. I have written the code for it but whenever I try to jump after I have just left the ground it doesn't jump as high as it would have if it jumped when it was touching the ground. Anyone have an idea why thisis happening ? the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class charatercontrol : MonoBehaviour
{
float touchGroundX;
bool isTouchingGround = false;
float touchGroundTime;
public Vector3 startPoint;
Rigidbody2D rb;
Vector3 vec;
bool canJump = true;
float horizontal;
bool canDash = true;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
void Start()
{
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (canJump && isTouchingGround)
{
rb.AddForce(new Vector2(0, 200));
canJump = false;
isTouchingGround = false;
}
else if (Time.time - touchGroundTime < 0.2)
{
if (touchGroundX > transform.localPosition.x)
{
rb.AddForce(new Vector2(0, 200));
canJump = false;
}
}
}
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
direction = 3;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
direction = 4;
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 3 && canDash)
{
rb.velocity = Vector2.up * dashSpeed;
canDash = false;
}
else if (direction == 4 && canDash)
{
rb.velocity = Vector2.down * dashSpeed;
canDash = false;
}
}
}
}
void FixedUpdate()
{
karakterHareket();
SpeedBoost();
}
void karakterHareket()
{
horizontal = Input.GetAxisRaw("Horizontal");
vec = new Vector3(horizontal / 10 * 16, rb.velocity.y, 0);
rb.velocity = vec;
}
void SpeedBoost()
{
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag != "wall")
{
touchGroundX = transform.localPosition.x;
isTouchingGround = true;
canJump = true;
canDash = true;
}
touchGroundTime = Time.time;
if (other.gameObject.tag == "spike")
{
transform.localPosition = startPoint;
}
if (other.gameObject.tag == "speedBoost")
{
rb.velocity = Vector2.up * dashSpeed;
}
}
void OnCollisionStay2D(Collision2D other)
{
if (other.gameObject.tag != "wall")
{
canDash = true;
}
}
void OnTriggerStay2D(Collider2D other)
{
if (other.tag == "speedBoost")
{
rb.AddForce(new Vector2(300, 500));
}
if (other.tag == "savepoint")
{
startPoint = transform.localPosition;
}
}
}
Your answer
Follow this Question
Related Questions
Help I have no errors but I can't jump still!! 1 Answer
Problem with sound 0 Answers
trying to make an enemy shoot a projectile at the player when the player enters the enemys range 2 Answers
I have a problem with coins 1 Answer
When I build my 2D game and set it to fullscreen, the resolution looks off 0 Answers