- Home /
Non slippery movement
Ive tried making non slippery movement. I'm new to unity, and there are a few problems with the code I am using.
public class PlayerControl : MonoBehaviour
{
public new Rigidbody2D rigidbody2D;
public float mSpeed = 5;
private bool moving;
void Start()
{
rigidbody2D = GetComponent<Rigidbody2D>();
}
Vector2 moveKeys = Vector2.zero;
void Update()
{
float moveX = Input.GetAxis("Horizontal");
if (moveX == 1 || moveX == -1)
{
moving = true;
}
else moving = false;
moveKeys = new Vector2(moveX, 0).normalized;
}
public void FixedUpdate()
{
if (moving == true)
{ rigidbody2D.velocity = new Vector3(moveKeys.x * mSpeed, 0);
Debug.Log("Function Called");
}
}
}
There is a delay for before I start moving and before I stop moving.
When I reach the ground, my character cannot move, only when in the air.
When moving in the air my vertical movement stops. This is most likely because in the line that moves my character's vector3's y is equal to zero, possible stopping vertical movement? I do not know a solution to this.
Please Help me
Answer by Llama_w_2Ls · 4 days ago
Use
Input.GetAxisRaw
or round your axis value to get it to reach 1 faster.
Your set velocity should include the current y velocity, like this:
rigidbody2D.velocity = new Vector3(moveKeys.x * mSpeed, rigidbody2D.velocity.y);
This will maintain any vertical velocity your rigidbody has.
And 3. There's no need to only set your velocity if you're moving. Just set it always to moveKeys.x and it will work the same.
Your answer
Follow this Question
Related Questions
Player won't stop moving while blocking 1 Answer
Player is Speeding up in collisions 0 Answers
Having trouble with a basic movement script. 2 Answers