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 gro420 · Sep 05, 2015 at 07:54 PM · c#unity 52d2d-platformerplatformer

Having a couple of problems regarding 2D Movement.

Hello there. I'm quite new to coding so these are some nooby questions. I am making a 2D Platformer and I have the movement setup but I have a couple of things that annoy me.

  1. When you are moving and then you take your finger off the key, instead of stopping completely and immediately, it slides for a second or so before stopping. I am not 100% sure how to make it just instantly stop.

  2. The player can stick to walls, meaning it can pretty much climb walls. I want it to just fall when it hits a wall, but I am not 100% sure how to do that.

Here is my code:

public float jumpHeight; public float moveSpeed;

 public Transform groundCheck;
 public float groundCheckRadius;
 public LayerMask whatIsGround;
 private bool grounded;
 private bool doubleJumped;
 // Use this for initialization
 void Start () {
 
 }

 void FixedUpdate(){
     grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
 }
 
 // Update is called once per frame
 void Update () {

     if (grounded) {
         doubleJumped = false;    
     }
                     

     if (Input.GetKeyDown (KeyCode.W)&& grounded ) {
         GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);    
         Jump ();
     }

     if (Input.GetKeyDown (KeyCode.W) && !doubleJumped && !grounded ) {
         GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
         Jump ();
         doubleJumped = true;
     }

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

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

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
2
Best Answer

Answer by Cam Edwards · Sep 05, 2015 at 10:53 PM

I'm making a similar game at the moment, and have just come across both of these problems.

Your first problem is relatively easy to solve. You are already setting the velocity of the GameObject's Rigidbody if the player is pressing the A or D keys, so all you need to do to stop instantly is to restructure your existing movement code a bit, to look like this:

 if (Input.GetKey (KeyCode.D)) 
 {
          // Your code to move left
 } 
 else if (Input.GetKey (KeyCode.A)) 
 {
         // Your code to move right
 }
 else
 {
     GetComponent<Rigidbody2D>().velocity = new Vector2 (0, GetComponent<Rigidbody2>().velocity.y);
 }

This means that if you are pressing A, you move left, or if you are pressing D you move right, but if you press neither, your velocity in the X direction is set to nothing.

It's worth pointing out that you are calling GetComponent() a lot in your code. You probably wont notice any performance loss by doing this a few times, but if you are constantly searching for components you might find your game slowing down. A better way to do this is to create a variable to hold your rigid body, and only search for it once.

 private RigidBody2D body;
 
 void Start()
 {
   body = GetComponent<Ridigbody2D>();
 }
 
 void Update()
 {
   ...
    body.velocity = new Vector2(...)
    ...
 }


For your second question, I'm still working on that! I've found that the 2D asset package has a similar character controller to what I'm looking for, you can import it into your own project to have a look. They stop the player sticking to walls by adding an extra BoxCollider2D to edges of platforms, and give it a physics material with 0 friction.

Comment
Add comment · Show 4 · 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 gro420 · Sep 05, 2015 at 11:32 PM 0
Share

Hm. After I added the line : else { body.velocity = new Vector2 (-0, body.velocity.y); } my "D" key did not want to do anything, and it said that there were no errors in the code. Thank you though. I'm going to try and fix this, I am not sure why it is like this.

avatar image Cam Edwards · Sep 06, 2015 at 09:40 AM 1
Share

It sounds like regardless of whether you press D or not, velocity.x is set to 0. Check your if statement, note that I changed your

if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))

line to read

else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))

If that isn't the problem, I think I'd need to see all of your code to work out what's wrong,

avatar image gro420 · Sep 06, 2015 at 10:13 AM 1
Share

@Cam Edwards I didn't notice that, that was the actual problem. Thanks again for helping, I really appreciate it.

Edit: I've also found out how to stop my character from sticking to walls. Create a new Physics2D $$anonymous$$aterial and then change the friction to 0. After that just assign it to your box collider(s).

avatar image Cam Edwards · Sep 06, 2015 at 10:56 AM 0
Share

Well, that's a great point! I had tried that originally before I added the code to stop instantly, but I started skidding all over the place as if I was on ice. That's not a problem if you stop your character yourself. Thanks for fixing that problem for me!

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

29 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

Related Questions

why when the player position changed, the player get stuck in that positon,Why when i change the Player position to an object, the player get stuck 0 Answers

Trouble with directing launched projectiles in Unity 2D 0 Answers

Trouble with controlling the direction of projectiles with C# code 1 Answer

Teleport 2D Character on TriggerEnter 2 Answers

Unity jump from one canvas scene to another scene with canvas 2 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