2d Movement without gravity(How)

Hi
I have a problem. I would like a character to move left and right but without gravity. I tried the character controller and various movement scripts but they all use gravity.
I just need something that moves left and right at a certain speed. How do I accomplish this

If you are using a rigidbody, you can set velocity yourself, or you can use the character controller and the “Move” function instead of the SimpleMove function.

but I don’t want to use a rigid body I don’t need gravity

A rigidbody moves objects with velocity and can ignore gravity if you uncheck the useGravity toggle.

Just use rigidbody.velocity.

I’ve found a way of doing this using MovePosition, geting the actual position X Y and adding de speed I want to the new position. I hope it helps somebody, cuz I searched for a long time.

using UnityEngine;
using System.Collections;

public class playerController : MonoBehaviour
{

    public float speed;             //Floating point variable to store the player's movement speed.

    private Rigidbody2D player;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
    public float forceX = 0 , forceY = 0 ;
    // Use this for initialization
    void Start()
    {
        //Get and store a reference to the Rigidbody2D component so that we can access it.
        player = GetComponent<Rigidbody2D>();
    }

    //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    void FixedUpdate()
    {


        if (Input.anyKey)
        {
           

                if (Input.GetKey("up"))
                { forceX = 0; forceY = speed; }
                if (Input.GetKey("down"))
                { forceX = 0; forceY = -speed; }
                if (Input.GetKey("left"))
                { forceX = -speed; forceY = 0; }
                if (Input.GetKey("right"))
                { forceX = speed; forceY = 0; }

                float playerX = player.position.x;
                float playerY = player.position.y;

                player.MovePosition(new Vector2(playerX + forceX, playerY + forceY));
           
        }
       
       

    }
}
3 Likes

Thx! I had the same problem.