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 mealphamale · Apr 21, 2014 at 03:07 PM · player2d-platformer2d-physics

Help needed with 2D Player Constant movement

Hi, I'm trying to make a 2D Platformer game and I'm stuck with this problem.

I've check with the source code of many 2D Platformer games but still my problem is unresolved even after 15 days. I decided to ask it here, I hope you guys will help a bit.

The Problem:

How can I move a 2D Player in X-axis in constant speed irrespective upward slope and downward slope? When the player approaches upward slope his speed decreases on X-axis, and when he approaches downward slope his speed increases on X-axis. I tried many things but it doesn't help.

Here is the inspector view of the player:

alt text

Here is the code: (Any Help?)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControllerScript : MonoBehaviour {
 
     public Transform groundCheck;
     bool grounded = false;
     float groundRadius = 0.1f;
     public LayerMask whatIsGround;
     float GetAxis;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     //    float move = Input.GetAxis ("Horizontal");
         GetAxis = Input.GetAxisRaw("Horizontal");
         rigidbody2D.gravityScale = GetAxis == 0 ? 0f : 2f;
         rigidbody2D.velocity = new Vector2 (1 * 3, rigidbody2D.velocity.y);
 
         //    transform.position = transform.forward * 10;
     }
 
     void FixedUpdate()
     {
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 
         if(grounded)
         {
             //Physics2D.gravity = new Vector3(0, 0, 0);
             print ("gravity is zero");
         }
 
 
     }
 }



2dplayer.png (49.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 Rphysx · Apr 21, 2014 at 03:47 PM

If your goal is to move at the same x-speed even if you're approaching an upward/downward slope my first approach would be changing the 2Dmaterial of the slope, and setting it's friction to 0 instead of 0.4(default from unity) To do this, you need to first create a new Physics2D material (Assets/create/physics2Dmaterial) set the material friction to 0 and then pass it to the "Material" parameter of your 2D box Collider.

As soon as you'll do this, you should notice that on an upward slope, if the character is not moving (getAxisRaw == 0) gravity will make you fall back a little time by time. To avoid this set the gravityScale to be zero at each update if getAxisRaw is zero

 float GetAxis;
 
 void Update () 
     {
         GetAxis = Input.GetAxisRaw("Horizontal");
         rigidbody2D.gravityScale = GetAxis == 0 ? 0f : 1f;
         //If getAxis is zero, gravity scale is set to 0 elseway 
         //gravity will be set to 1
     }
Comment
Add comment · Show 7 · 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 mealphamale · Apr 21, 2014 at 04:18 PM 0
Share

Hey thanks for replying. I've put "Slippery" material with 0 Friction and 0 Bounciness to the player's box collider. But now the problem is when the player has reached the upward slope he continues to go upwards and it never falls back to the platform. I've updated the code now. Please have a look.

avatar image mealphamale · Apr 21, 2014 at 05:44 PM 0
Share

Any help guys?

avatar image Rphysx · Apr 21, 2014 at 05:58 PM 0
Share

There's a error in your code : rigidbody2D.velocity = new Vector2 (1 * 3, rigidbody2D.velocity.y); if you put 1 * 3 the player at each update will always move to the right, change this code this way rigidbody2D.velocity = new Vector2 (GetAxis * 3, rigidbody2D.velocity.y); now you should be able to move freely trought the scene

avatar image mealphamale · Apr 21, 2014 at 06:59 PM 0
Share

I changed the line. Again I'm back to square one.

avatar image mealphamale · Apr 21, 2014 at 07:01 PM 0
Share

$$anonymous$$y requirement is that player should move on x axis without any triggers automatically, so there is no point of controlling it. When I keep this line: rigidbody2D.velocity = new Vector2 (1 * 3, rigidbody2D.velocity.y);

Everything works but the player keep moving upwards even if he has already reached the peak point of a hill. Any help?

Show more comments
avatar image
0

Answer by mealphamale · Apr 21, 2014 at 08:49 PM

Okay.. Forget the problem. I bought a unity plugin which solves all of my problems with 2D player controller. Thanks

Comment
Add comment · Show 1 · 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 ayushsingla991 · Mar 29, 2017 at 06:10 AM 0
Share

Can you please provide the link to the asset?

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

23 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

Related Questions

snap the character to hit.normal.x of the ground 0 Answers

How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer

[2D] Character keeps sliding when I add velocity to the rigidbody2D? 1 Answer

Doodle Jump Breakable Platform 2 Answers

How do I make a physics based elevator? 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