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 /
  • Help Room /
avatar image
0
Question by giants_105 · Apr 05, 2021 at 04:44 PM · walljump

walljump doesnt work!

Hi I'm new to Unity and I'm having trouble with coding a walljump. For some reason, when I press the jump button, sometimes the walljump just doesn't work. The x-movement works fine but the y-movement doesn't work. It works sometimes but other times only the x works and i still fall downwards. I changed the gravity a few times but I fell at the same rate. I also changed the y-Walljump force but it was the same. PS: You only need to really look at the lines of code all the way to "private void awake".

My Code:

using UnityEngine; using UnityEngine.Events;

public class CharacterController2D : MonoBehaviour { [SerializeField] private float m_JumpForce = 400f;
[Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
[SerializeField] private bool m_AirControl = false;
[SerializeField] private LayerMask m_WhatIsGround;
[SerializeField] private Transform m_GroundCheck;
[SerializeField] private Transform m_CeilingCheck;
[SerializeField] private Collider2D m_CrouchDisableCollider;

 const float k_GroundedRadius = .2f; 
 private bool m_Grounded;           
 const float k_CeilingRadius = .2f; 
 const float k_FrontRadius = .2f;
 private Rigidbody2D m_Rigidbody2D;
 private bool m_FacingRight = true;  
 private Vector3 m_Velocity = Vector3.zero;
 
 bool isTouchingFront;
 public Transform FrontCheck;
 bool wallSliding;
 public float wallSlidingSpeed;

 bool wallJumping;
 public float xWallForce;
 public float yWallForce;
 public float wallJumpTime;

 [Header("Events")]
 [Space]

 public UnityEvent OnLandEvent;

 [System.Serializable]
 public class BoolEvent : UnityEvent<bool> { }

 public BoolEvent OnCrouchEvent;
 private bool m_wasCrouching = false;

 private void Update()
 {
     isTouchingFront = Physics2D.OverlapCircle(FrontCheck.position, k_FrontRadius, m_WhatIsGround);

     if (isTouchingFront == true && m_Grounded == false) 
     {
         wallSliding = true;
     }
     else
     {
         wallSliding = false;
     }

     if (wallSliding)
     {
         m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, Mathf.Clamp(m_Rigidbody2D.velocity.y, -wallSlidingSpeed, Input.GetAxisRaw("Horizontal") * 4));
     }

     if (Input.GetButtonDown("Jump") && wallSliding == true)
     {
         wallJumping = true;
     }
     else
     {
         wallJumping = false;
     }

     if (wallJumping)
     {
         m_Rigidbody2D.velocity = new Vector2(xWallForce * -Input.GetAxisRaw("Horizontal"), yWallForce);
     }

 
 }

 private void Awake()
 {
     m_Rigidbody2D = GetComponent<Rigidbody2D>();

     if (OnLandEvent == null)
         OnLandEvent = new UnityEvent();

     if (OnCrouchEvent == null)
         OnCrouchEvent = new BoolEvent();
 }

 private void FixedUpdate()
 {
     bool wasGrounded = m_Grounded;
     m_Grounded = false;

     
     Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
     for (int i = 0; i < colliders.Length; i++)
     {
         if (colliders[i].gameObject != gameObject)
         {
             m_Grounded = true;
             if (!wasGrounded)
                 OnLandEvent.Invoke();
         }
     }
 }


 public void Move(float move, bool crouch, bool jump)
 {
     
     if (!crouch)
     {
         
         if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
         {
             crouch = true;
         }
     }

     
     if (m_Grounded || m_AirControl)
     {

         
         if (crouch)
         {
             if (!m_wasCrouching)
             {
                 m_wasCrouching = true;
                 OnCrouchEvent.Invoke(true);
             }

             
             move *= m_CrouchSpeed;

             
             if (m_CrouchDisableCollider != null)
                 m_CrouchDisableCollider.enabled = false;
         } else
         {
             
             if (m_CrouchDisableCollider != null)
                 m_CrouchDisableCollider.enabled = true;

             if (m_wasCrouching)
             {
                 m_wasCrouching = false;
                 OnCrouchEvent.Invoke(false);
             }
         }

         
         Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
         
         m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
         
         if (move > 0 && !m_FacingRight)
         {
             Flip();
         }
         
         else if (move < 0 && m_FacingRight)
         {
             Flip();
         }
     }
     
     if (m_Grounded && jump)
     {
         m_Grounded = false;
         m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
     }
 }


 private void Flip()
 {
     m_FacingRight = !m_FacingRight;

     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }

}

Please help!

Note: I changed it to make the new vector2 into a "add force" but it still didn't work so here's new code

private void Update() { isTouchingFront = Physics2D.OverlapCircle(FrontCheck.position, k_FrontRadius, m_WhatIsGround);

     if (Input.GetKey("right"))
     {
         m_lastHorizontal = 1;
     }
     else if (Input.GetKey("left"))
     {
         m_lastHorizontal = -1;
     }

     if (isTouchingFront == true && m_Grounded == false) 
     {
         wallSliding = true;
     }
     else
     {
         wallSliding = false;
     }

     if (wallSliding)
     {
         m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, Mathf.Clamp(m_Rigidbody2D.velocity.y, -wallSlidingSpeed, Input.GetAxisRaw("Horizontal") * 4));
     }

     if (Input.GetButtonDown("Jump") && wallSliding == true)
     {
         wallJumping = true;
         Invoke("SetJumpToFalse", wallJumpTime);
     }
     else
     {
         wallJumping = false;
     }

     if (wallJumping)
     {
         print("jumping!!!!");
         m_Rigidbody2D.AddForce(new Vector2(xWallForce * -m_lastHorizontal, yWallForce), ForceMode2D.Impulse);
         wallJumping = false;
     }

 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by giants_105 · Apr 06, 2021 at 07:07 AM

i did it!

i just needed to put flip() after adding the force for the walljump :)

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

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

156 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 avatar image avatar image avatar image avatar image

Related Questions

I can not get my character to crouch? Character not sliding on walls? 0 Answers

How to move upward with wall jump and not be pulled down too quickly? 0 Answers

I have a walljump script but the walljump doesn't work right can someone help me? 0 Answers

Help with walljump/ wallstick script!,Help with walljump/ wallstick script! 0 Answers

How can my wallrun script double wallrun ? 0 Answers


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