• 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 Jack-Howard · Jul 29, 2016 at 12:30 PM · c#2dscripting problem2d-platformerwall jump

I cant get wall jumping to work in my 2D platformer (C#)

I coded this script in C#

I've pasted below the code for my entire player controller. Jumping and double jumping works fine but whenever i wall jump it just go's up the wall but doesn't push away from it. I am trying to get wall jumping similar to supermeatboy.

I thought it had something to do with the "moveVelocity = 0f;" which would just stop the player in mid air but i don't think that is the only problem because i put that in a "If (WallJumped == false)" so that if you did wall jump then it shouldn't stop your velocity. Any help would be greatly appreciated thanks.

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public float moveSpeed;
 private float moveVelocity;
 public float jumpHeight;
 public float awayHeight;

 public Transform groundCheck;
 public float groundCheckRadius;
 public LayerMask whatIsGround;
 private bool grounded;

 public Transform wallCheck;
 public float wallCheckRadius;
 public LayerMask whatIsWall;
 private bool onWall;

 private bool doubleJumped;

 private Animator anim;

 private PlayerController player;
 //public GameObject respawnParticle;
 private Rigidbody2D rigid;

 private bool wallJumped;



 // Use this for initialization
 void Start()
 {
     anim = GetComponent<Animator>();

     player = FindObjectOfType<PlayerController>();
     player.GetComponent<Renderer>().enabled = true;
     //Instantiate (respawnParticle, player.transform.position, player.transform.rotation);
     rigid = GetComponent<Rigidbody2D>();
     wallJumped = false;
 }



 void FixedUpdate()
 {

     grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
     onWall = Physics2D.OverlapCircle(wallCheck.position, wallCheckRadius, whatIsWall);
 }

 // Update is called once per frame
 void Update()
 {

     if (Input.GetKeyDown(KeyCode.W) && grounded)
     {
         Jump();
     }

     if (Input.GetKeyDown(KeyCode.W) && !doubleJumped && !grounded)
     {
         Jump();
         doubleJumped = true;
     }

     if (grounded)
     {
         doubleJumped = false;
     }
     anim.SetBool("Grounded", grounded);

     if (onWall)
     {
         if (wallJumped == false)
         {
             doubleJumped = false;
         }
         else
         {
             doubleJumped = true;
         }

         if (Input.GetKeyDown(KeyCode.W) && doubleJumped == false)
         {
             JumpAwayFromWall();
             wallJumped = true;
         }


         if (rigid.velocity.y < 0)
         {
             GetComponent<Rigidbody2D>().gravityScale = 2f;
         }
         else
         {
             GetComponent<Rigidbody2D>().gravityScale = 3.5f;
         }


     }
     else
     {
         GetComponent<Rigidbody2D>().gravityScale = 3.5f;
         wallJumped = false;
     }
     anim.SetBool("OnWall", onWall);

     moveVelocity = 0f;

     anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));

     if (GetComponent<Rigidbody2D>().velocity.x > 0)
         transform.localScale = new Vector3(0.25f, 0.25f, 1f);
     else if (GetComponent<Rigidbody2D>().velocity.x < 0)
         transform.localScale = new Vector3(-0.25f, 0.25f, 1f);

     if (Input.GetKey(KeyCode.D))
     {
         //GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
         moveVelocity = moveSpeed;
     }

     if (Input.GetKey(KeyCode.A))
     {
         //GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
         moveVelocity = -moveSpeed;
     }


     GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
 }

 public void Jump()
 {
     GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
 }

 public void JumpAwayFromWall()
 {
     GetComponent<Rigidbody2D>().AddForce(new Vector2(awayHeight, jumpHeight));

     //GetComponent<Rigidbody2D>().velocity = new Vector2(awayHeight, jumpHeight);

     //GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.y, jumpHeight);
     //GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);

     //GetComponent<Rigidbody2D> ().velocity = new Vector2(-knockback, knockback);
 }

 void OnCollisionEnter2D(Collision2D other)
 {
     if (other.transform.tag == "MovingPlatform")
     {
         transform.parent = other.transform;
     }
 }

 void OnCollisionExit2D(Collision2D other)
 {
     if (other.transform.tag == "MovingPlatform")
     {
         transform.parent = null;
     }
 }

 private void Flip()
 {
     // Multiply the player's x local scale by -1.
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }

}

Comment
Add comment · Show 3
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 Jack-Howard · Jul 29, 2016 at 12:41 PM 0
Share

alt text

here is a screenshot of the values I gave to my player

jumphelpscreenshot.png (281.0 kB)
avatar image Jack-Howard · Jul 29, 2016 at 03:34 PM 0
Share

bump......

avatar image ScaniX · Jul 29, 2016 at 03:34 PM 1
Share

Probably you have just to change the direction of your horizontal force. If the character runs towards the right side, it should be negative to push away to the left.

You can either add an if to change the value depending on the direction you are facing or use AddRelativeForce() (although I don't know if setting the scale.x is actually flipping the x-axis of the transform as well).

 GetComponent<Rigidbody2D>().AddForce(new Vector2((transform.localScale.x < 0 ? -1 : 1) * awayHeight, jumpHeight));

Also you might have to use a different Force$$anonymous$$ode or a higher horizontal force if the player has still some horizontal velocity that works against it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CnrGames · Aug 01, 2016 at 04:01 AM

lso you might have to use a different ForceMode or a higher horizontal force if the player has still some horizontal velocity that works against it.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Add audio. 1 Answer

I am trying to spawn and move a asteroid but the spawnner script doesnot access the script attached to the asteroid. 1 Answer

Boss Health Bar 1 Answer

Jumping from special jump orb 1 Answer

How to check if an object hits the ground hard enough then add explosive force around it (2D) 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