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);
}
}
}