- Home /
How to reduce speed while moving left and right in 2D platform
Hello, I'm new in Unity, and I've just started making my own game - very simple 2D platformer, following a tutorial. I'd like to change the speed of my charakter while it is midair. Altrough I want to change the speed only while moving left and right. I don't want to change the speed of going up or falling down. Basically I'd like to reduce speed in x-axis only. I've already tried a few thing, but nothing has worked and probably it'd better, if I won't post it here. : )My script:
public float runSpeed;
public float jumpHeight;
public Transform groudCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groudCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update ()
{
anim.SetBool("Grounded", grounded);
if (Input.GetKeyDown(KeyCode.W) && grounded)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(runSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-runSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
}
Comment
Your answer
Follow this Question
Related Questions
Problem between my player jump and player movement 1 Answer
How to fix horizontal movement code causing fast speed bug? 0 Answers
Accelerate slowdown Player movement 0 Answers
Unity Character Not Moving Midair 2 Answers
Rotation used as movement 4 Answers