Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 leemurs · Dec 23, 2019 at 07:20 PM · platformerplayer movement

Help a novice with wall jumping

I want to get back into using unity and I've been trying to add wall jumping into an old school project and I am at a loss with how to do it as I seem to have forgotten what little I have learned. I have found some solutions but don't know how to implement it into my own code without breaking everything. Here is the code for the the unit i want to be able to have wall jump. Any help is greatly appreciated.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour {

 Rigidbody2D rb;
 public float speed = 30;
 public bool jump = false;
 public bool grounded = true;
 public Transform groundCheck;
 public LayerMask mask;

 // Start is called before the first frame update
 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame
 void Update()
 {
     //is our player grounded?
     RaycastHit2D rh = Physics2D.Linecast(transform.position, groundCheck.position, mask);
     //RaycastHit2D rh = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
     if (rh.collider != null)
     {
         grounded = true;
     }
     else
     {
         grounded = false;
     }

     if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         jump = true;
     }
 }

 private void FixedUpdate()
 {
     float h = Input.GetAxis("Horizontal");
     rb.AddForce(h * Vector2.right * speed);

     if (jump && grounded)
     {
         rb.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
         jump = false;
     }
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     SceneManager.LoadScene("SampleScene");
 }

}

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
1

Answer by BillehBawb · Dec 23, 2019 at 11:30 PM

There are a lot of different ways to do this, but the simplest would probably be to use collisions. Make sure your wall and your player have colliders (like a BoxCollider2D) and Rigidbody2Ds. Then you can make a "Wall" tag, give it to any walls you want to be wall-jumpable, and do something like this in your player script.

 private void OnCollisionEnter2D(Collision2D collision)
 {
     // Check if the object the player is against is a wall, if they are off the ground and if up is being pressed
     if (collision.collider.tag.Equals("Wall") && !grounded && jump)
     {
         // Add velocity to the player to push them off the wall. Put in your own values for xforce and yforce
         rb.AddForce(new Vector2(xforce, yforce));
     }
 }
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

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

Ground check BoxCast is detecting when player hits bottom of platform. UNITY 2D C# 0 Answers

Why does my platform move when I press my directional keys when it does not have the PlayerController script? 1 Answer

My Unity Rigidbody 2D character fall slowly after a jump,Unity RigidBody Character falls slowly 0 Answers

Unity Player bumps when falls and dash 0 Answers

How to make player fall through platforms? 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