- Home /
how does SimpleMove apply gravity?
Calling all programer gurus!!
I am new to programming (started about 7 months) and I am starting to get the hang of it now. I am trying to build my own character controller so I can understand how to apply physics etc,
My question is how does .SimpleMove apply gravity?
forward = transform.TransformDirection (Vector3.forward);
curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove (forward * curSpeed);
this section of the cade is where the gravity is applied. But I don't understand how.
Can anyone explain this to me? The full code can be found here http://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html
Thanks
As a guess, they are using something similar to what is done in the sample script for CharacterController.$$anonymous$$ove(). You might put two character side by side using $$anonymous$$ove() and Simple$$anonymous$$ove() and compare the jump behavior.
Yea, I'm starting to think it's something "under the hood" kind of thing. Similar to the way it doesn't need Time.deltaTime .. if that makes sense
the main reason why I wanted to know because I like the Simple$$anonymous$$ove() feel, but I couldn't add jump very easily.. $$anonymous$$ove() is easier to add jump but I didn't want strafing. I also wanted to add different animations for backward and forward motion and apply sprint... this is what I got, and works fine!
There is probably better ways to optimise this script but I feel like a big boy programmer now :)
public float speed = 10.0F; public float sprintSpeed = 20.0f; public float rotateSpeed = 3.0f; public float jumpHeight = 8.0F; public float gravity = 20.0F;
Vector3 moveDirection = Vector3.zero;
CharacterController controller;
bool sprint = false;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
// for movement forward
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
// for adding animations to movement direction
if (moveDirection.z > 0)
Debug.Log ("moving forward");
else if (moveDirection.z < 0)
Debug.Log ("moving backward");
else
Debug.Log ("idle");
// spins ins$$anonymous$$d of straif
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
moveDirection = transform.TransformDirection(moveDirection);
// toggles between speed and sprintSpeed
if(sprint == true)
moveDirection *= sprintSpeed;
else
moveDirection *= speed;
// apply sprint which is set up to "left shift"
if (Input.GetButton ("Run"))
sprint = true;
else
sprint = false;
// apply jump
if (Input.GetButton("Jump"))
moveDirection.y = jumpHeight;
}
// apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// apply movement
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
Answer by Brogan89 · May 14, 2019 at 08:20 PM
The answer to the question can be found here
"SimpleMove takes the speed as parameter and will move the character accordingly. On top of that, the character will respond to gravity. That’s the only physic you’ll get with the Character Controller. The downside is that the Y axis velocity is ignored by this method"
Answer by gribbly · Oct 29, 2014 at 01:40 AM
SimpleMove is not applying gravity. The physics system is applying gravity via the rigidbody component attached to the gameobject.
But I am not using a rigidbody. I am using a characterController. Plus, in the API of Simple$$anonymous$$ove it says "Gravity is automatically applied"
Answer by stjernerlever · May 14, 2019 at 01:37 PM
Did you ever find out? :)
I am currently trying to make gravity apply to my NPCs. They wander about with a script that uses SimpleMove, and they will fall towards the ground, just very slowly. This means they can walk down a hill, but if they attempt to run down the hill, they will run off the terrain and glide through the air.
oh man this was a long time ago lol. Yea, pretty much the only physics parameter applied with CharacterController.Simple$$anonymous$$ove()
is gravity.
I would suggest using a Rigidbody though. It will be easier to set up, CharacterController you have to do a lot of the code yourself. Which can be an up side because you'll have more control.
Checkout this link https://medium.com/ironequal/unity-character-controller-vs-rigidbody-a1e243591483
Your answer
![](https://koobas.hobune.stream/wayback/20220613170220im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Any way to disable SimpleMove's sliding? 1 Answer
Charactercontroller.move affecting rotation 1 Answer
Problem with CharacterController.SimpleMove 0 Answers
Move() function doesn't work on character controller! 0 Answers
SimpleMove with Chasing 1 Answer