Character moves downwards without gravity

Hi,

I have been using Unity for a while, I used to use any of the default scripts to control player movement but I have now decided to write my own from scratch. I have written a very basic script, but I can´t understand what´s happening.
In my scene I only have the player (a cube) with a character controller and a plane as the floor. The script just takes input from the left and right keys, multiplies it by deltaTime and passes it to the move function. That works fine, however, in addition to this, the player also moves down until it hits the floor. I can´t understand why this is happening since I´m not applying any gravity, also, it only moves downwards when the left or right keys are pressed, if I release them it stops. I´m printing the values of the velocity vector I´m passing to the Move function and the y component is always 0. Any idea why this is happening? Here is the script:

public class SimpleMove : MonoBehaviour {
	
	public  float   acceleration;
	public  float   maxVel;
	
	private Vector3 acc;
	private Vector3 vel;
	
	private CharacterController controller;
	private CollisionFlags collisionFlags;
	

	// Use this for initialization
	void Start () {
		controller = this.GetComponent<CharacterController>();
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		acc = new Vector3(Input.GetAxis("Horizontal"),0,0);
		acc *= acceleration;
		
		if (acc.x == 0.0f)
		{
			vel = Vector3.zero;
		}
		else
		{	
			vel += acc * Time.deltaTime;
		}
		
		BoundVelocity();
		Debug.Log(vel);
		collisionFlags = controller.Move(vel);
	}
	
	void BoundVelocity()
	{
		if (vel.x < 0)
		{
			vel.x = Mathf.Max(vel.x, -maxVel);
		}
		else
		{
			vel.x = Mathf.Min(vel.x, maxVel);
		}
	}
}

You’ve added a CharacterController. Character controllers add physics i.e. gravity. Change your code to be character controller independent!

CasualTGames, character controllers by themselves only move and collide with things that have collisions in the level. I think you might have it mixed up with a Rigid Body. The Rigid Body is the one that adds the “gravity effect” to an object. I have a character controller setup in my own game, and with no rigid body, they definitely do not add gravity or any movement on their own.

willqp, is there a Rigid Body attached to your player object cube? If so, you will need to deactivate or remove it from the object to prevent it’s own physics from taking over movement.

One other easy thing to miss is to make sure that your movement is based on world coordinates instead of object coordinates(or the other way, depending on what you are trying to accomplish). It may simply be moving the object along it’s own plane that may not line up with the level’s “world axis”.

I use java instead of C code, so I’ll have to leave the code itself to someone else who can read it better then me. Just thought I’d toss some tips of what got me messing up on this when I started out.

There are no rigidbodies. Also, it only moves down when i’m pressing the left or right keys (while at the same time moving right or left, which is the correct behaviour)

@willqp When posting code can you wrap it with the tag, or highlight the code and click the # when in Advanced posting.

@Aurore Thanks for the tip. Any idea why the character controller moves down when I call the Move function?