CharacterController how to disable acceleration of falling when moving in the air?

I have a scene, there is a cube with attached CharacterConrtoller and my script which you can see below.

Cube falls as wanted, but if I press any of WASD or arrows, it starts to fall much more faster. How to deal with it?

Watch screenshot of my scene. No scripts attached to Camera or Plain (click on 1.png in the bottom for full size)

Screenshots of Y-change without anything pressed and with W pressed:

http://i008.radikal.ru/1409/28/4c55ae5789af.png

http://i023.radikal.ru/1409/1e/0882ae1c41d4.png

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
	
	public float speed = 6.0F;
	public float gravity = 0.0F;
	private Vector3 moveDirection = Vector3.zero;
	// Update is called once per frame
	void Update () {

		CharacterController controller = GetComponent();

		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;			

		moveDirection.y -= gravity * Time.deltaTime;
		controller.Move(moveDirection * Time.deltaTime);
	
	}
}

Nothing in this code suggests why it would. In fact, it's copied straight from the scripting reference. Something else must be changing your Gravity values at runtime.

I will add a screenshot now...

This is ridiculous. Your code applies gravity linearly...because every frame you reset moveDirection.y to 0, then apply gravity to it. I'm going to ask for a favour, @DealWithMeal. Put in Debug.Log( moveDirection.y ) after controller.Move and take a screenshot of the results when you don't press any buttons, and when you do press buttons.

@Bluntweapon added two screenshots... it is super weird, lol

Hmm, both screenshot points to about -0.8 for moveDirection.y, so it's not your code that's doing it; if fact, you've proved my point, and are only applying gravity linearly, so your cube should be moving downwards at a linear rate.

1 Answer

1

Try moving anything physics related to a FixedUpdate() function.

But Move() function must not use physics, but it seems that it uses.