- Home /
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<CharacterController>();
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.
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, @DealWith$$anonymous$$eal. Put in Debug.Log( moveDirection.y )
after controller.$$anonymous$$ove
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.
Answer by StianC · Sep 17, 2014 at 08:08 PM
Try moving anything physics related to a FixedUpdate() function.
But $$anonymous$$ove() function must not use physics, but it seems that it uses.
Your answer
Follow this Question
Related Questions
Hilarious Gravity defect i don't know how to fix 1 Answer
Use Mouse as Direction for Character Movement? 0 Answers
character gravity problem 1 Answer
Change gravity point on trigger 0 Answers
Character Controller / Charactor Motor has no inputs? 1 Answer