shmifful
shmifful

Reputation: 55

How do I keep a constant velocity in Unity 2d?

I am quiet new to Unity and C#. I am tring to develop my first game, and a lot of the syntax confuses me. I am trying to move my sprite in the x axis using force, when "a" or "d" or the arrow keys are pressed, but it seems that my sprite doesnt move at a constant velocity.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class playerMovement : MonoBehaviour
{
    public float vel = .5f;
    public float jumpVel = 10f;
    public bool isGrounded = false;
    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = transform.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        //Vector3 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0f,0f);
        //transform.position += move * Time.deltaTime * vel;
        if(Input.GetAxisRaw("Horizontal") == 1){
            rb.AddForce(Vector2.right*vel, ForceMode2D.Force);
        }else if(Input.GetAxisRaw("Horizontal") == -1){
            rb.AddForce(Vector2.right*-vel, ForceMode2D.Force);
        }
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true){
            rb.velocity = Vector2.up * jumpVel;
        }

    }
}

Upvotes: 1

Views: 986

Answers (1)

That is because you are adding force to it using rb.AddForce, it is adding velocity every time it plays, for player movement, i recommend using the CharacterMovement component, but if you want to use Rigidbody2d the best solution i can think of is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class playerMovement : MonoBehaviour
{
    public float vel = .5f;
    public float jumpVel = 10f;
    public bool isGrounded = false;
    private Rigidbody2D rb;
    public float maxvel = 10f;

    // Start is called before the first frame update
    void Start()
    {
        rb = transform.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        //Vector3 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0f,0f);
        //transform.position += move * Time.deltaTime * vel;
        if(Input.GetAxisRaw("Horizontal") == 1 &&rb.velocity.x<=maxvel){
            rb.AddForce(Vector2.right*vel, ForceMode2D.Force);
        }else if(Input.GetAxisRaw("Horizontal") == -1 && rb.velocity.x>=-maxvel){
            rb.AddForce(Vector2.right*-vel, ForceMode2D.Force);
        }
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true){
            rb.velocity = Vector2.up * jumpVel;
        }

    }
}

so, in this answer the float maxvel (that stands for maximum velocity), this float is used to know where you want a idoneus velocity (you should adjust that by yourself, i just gave some reference for the code to work), i also call rb.velocity.x to checking the actual velocity on the horizontal axis, i decided not to change directly that velocity because that would cause it to go from 0 to the velocity you put there, using addforce makes it work smoothly;

Upvotes: 2

Related Questions