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");
}
}
}