- Home /
Why my Character's wall jump looks like teleport?
Hi guys, I'm a beginner of Unity and don't have any background of coding, I learn it by unity learning and youtube only.
Now I have a problem of wall jump, my character can't jump smoothly opposite to the wall, it looks like teleport horizontally and didn't move upward like image below, here is my coding:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D rb;
public float speed = 20;
private bool facingRight = true;
public float jumpVelocity = 25;
private bool isGround;
public Transform groundCheck1;
public Transform groundCheck2;
public LayerMask defineGround;
private bool isWall;
public Transform wallCheckLeft;
public Transform wallCheckRight;
public float wallCheckRadius;
public float slideDown = -3;
public float wallGrabSpeed = 10;
public float wallGrabTime = 2;
private bool wallJumping = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
Vector2 dir = new Vector2(x, y);
if (x > 0 && facingRight == false)
{
Flip();
}
else if( x < 0 && facingRight == true)
{
Flip();
}
Walk(dir);
isGround = Physics2D.OverlapArea(groundCheck1.position, groundCheck2.position, defineGround);
isWall = Physics2D.OverlapCircle(wallCheckLeft.position, wallCheckRadius, defineGround) || Physics2D.OverlapCircle(wallCheckRight.position, wallCheckRadius, defineGround);
if (Input.GetButtonDown("Jump"))
{
Jump();
}
if (Input.GetButtonDown("Jump") && isGround != true && isWall == true)
{
wallJumping = true;
Invoke("SetwallJumpingToFalse", 0.01f);
}
if (wallJumping)
{
rb.velocity = new Vector2(-rb.velocity.x * speed, jumpVelocity);
}
if (isGround == true)
{
wallGrabTime = 2;
}
if (isWall == true && Input.GetButton("Horizontal"))
{
WallGrab();
}
}
private void Walk(Vector2 dir)
{
rb.velocity = (new Vector2(dir.x * speed, rb.velocity.y));
}
private void Jump()
{
if (isGround == true)
{
rb.velocity = (new Vector2(rb.velocity.x, jumpVelocity));
}
}
void SetwallJumpingToFalse()
{
wallJumping = false;
}
private void WallGrab()
{
if (Input.GetButton("Vertical") && wallGrabTime > 0)
{
rb.velocity = (new Vector2(rb.velocity.x, slideDown + wallGrabSpeed));
wallGrabTime -= Time.deltaTime;
}
}
private void Flip()
{
transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y);
facingRight = !facingRight;
}
}
Please forgive me why I don't use other solved answer's coding, I just want to know what's wrong to my coding, why it can't work smoothly and how should I solve this...thanks.
Answer by PlayCreatively · Aug 02, 2020 at 01:37 PM
I'm guessing this is the problem
if (wallJumping)
{
rb.velocity = new Vector2(-rb.velocity.x * speed, jumpVelocity);
}
You are telling the velocity to go in the opposite direction of which it's going. Doing that every frame would most likely give bizarre results. I know you set a timer for 0.01 seconds but that does not guarantee it only runs once. -rb.velocity.x
isn't what you want either. When you've hit the wall the value would be 0f, giving no detail as to where you came from. You need to split the isWall
into isWallRight
and isWallLeft
. From that decide which direction you want to leap to.
Answer by kaiswe94 · Aug 02, 2020 at 03:33 PM
Well, the list of videos below are I learn the wall jump coding:
https://www.youtube.com/watch?v=koDg2Mw1kEc&t=196s
https://www.youtube.com/watch?v=KCzEnKLaaPc&t=547s
Both they do is add minus for velocity.x, so...maybe the coding of isWall
is not the reason...or maybe I have something wrong to coding from the videos, can anyone help me and explain what's wrong of my coding?
Then I recommend you find tutorials by people who know how to code properly
This is what I do always...I learned this by Unity learning and youtube...those are tutorials for me. $$anonymous$$aybe you can provide me more source to learn. Beacause I can't understand why I followed the process from videos, my "wall jump" is different to thiers.
Check out GameGrind he goes through the basics of C# really well