- Home /
Remove Acceleration of Player
I wanna make a Movement Code for a Plataform 2D. I know that my code isn't good, but works good for movement, jump and Double Jump. I'm using RigidBody2D AddForce to Move and Jump, and using "Velocity X = 0" to remove acceleration. This worked, but now I want to add Wall Jump and I can't use AddForce or Velocity on X Axys cause the Velocity X is Constantly 0. Can I do anything or I need to change all my movement System?
(Sorry my English) Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlaRe : MonoBehaviour {
public int velR, puloR, puloWall;
public Rigidbody2D rigidB;
public bool direiRe;
//Teste Bools
public bool dJump, wJump, bJump;
public static int maxJump;
void Start () {
maxJump = 0;
direiRe = true;
rigidB = gameObject.GetComponent<Rigidbody2D>();
}
void Update () {
//rigidB.velocity = new Vector2(0, rigidB.velocity.y);
// Movimentos
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
if (!ConfereChao.colidiP || !direiRe)
{
MoveRe(); direiRe = true;
}
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
if (!ConfereChao.colidiP || direiRe)
{
MoveRe(-1); direiRe = false;
}
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (ConfereChao.colidiC || (maxJump == 1 && dJump))
{
if (!ConfereChao.colidiC)
{
rigidB.velocity = new Vector2(rigidB.velocity.x, 0);
}
rigidB.AddForce(Vector2.up * puloR);
maxJump++;
}
if (ConfereChao.colidiP && wJump && !ConfereChao.colidiC)
{
rigidB.velocity = new Vector2(rigidB.velocity.x, 0);
rigidB.AddForce(Vector2.up * puloWall);
}
}
// Sprite Lado
if (direiRe == true)
{
GameObject.Find("TextuRe").transform.rotation = Quaternion.Euler(0, 0, 0);
}
else
{
GameObject.Find("TextuRe").transform.rotation = Quaternion.Euler(0, 180, 0);
}
}
void MoveRe(int leftRight = 1){
rigidB.AddForce(Vector2.right*velR*leftRight);
}
}
Answer by danidani360 · Oct 11, 2020 at 01:01 AM
I'm not sure if this will help your jumping issue, but to change the movement of the player you can use "Input.GetAxisRaw". This will give you a value of -1 (left), 0 (not moving), or 1 (right) as the left and right buttons are pressed. Multiply this value by speed for the player to move. This way the player does not accelerate, but immediately hits the desired speed. Hopefully that resolves your issue!
Your answer
![](https://koobas.hobune.stream/wayback/20220613013442im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Disabling diagonal movement in Unity 5 2D? (using C#) 1 Answer
How to make cars rotate towards a waypoint while turning? 0 Answers
2D Faux Gravity using a Point Effector? 1 Answer
Rigid Body Movement (Mobile Issue) 3 Answers
Bounce into the air 1 Answer