Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kaiswe94 · Aug 02, 2020 at 12:18 PM · unity 2dbeginnerwall jump

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:

alt text

 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.

20200802-201151.gif (441.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PlayCreatively · Aug 02, 2020 at 05:54 PM 0
Share

Then I recommend you find tutorials by people who know how to code properly

avatar image kaiswe94 PlayCreatively · Aug 03, 2020 at 02:18 AM 0
Share

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.

avatar image PlayCreatively kaiswe94 · Aug 04, 2020 at 05:19 PM 0
Share

Check out GameGrind he goes through the basics of C# really well

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

trying to get a good wall jump for my 2d game 1 Answer

I really need a JumpPad script for a JumpPad in unity 2D 0 Answers

I have a problem with coins 1 Answer

Character teleports upwards instead of jumping smoothly Unity2D 2 Answers

Why does my swiping code not work? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges