- Home /
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:
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");
}
}
}
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
}
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.
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
$$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?
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
Your answer
Follow this Question
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