- Home /
My character fall down on the ground ?
Please I want your help! My character keeps falling down not through the ground, i did the rigidbody and i did the tick on the use gravity but it still falls... please help me!
public float Sp; public float gravity = 20.0F; private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// this code will make the character jump
if (Input.GetKeyDown ("space")) {
transform.Translate (Vector3.up * 400 * Time.deltaTime, Space.World);
}
moveDirection.y -= gravity * Time.deltaTime;
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += Vector3.right * Time.deltaTime * Sp;
}
// this code is to make the object move to the left in every second by pressing the left arrow
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += Vector3.left * Time.deltaTime * Sp;
}
// this code make the object position move forward in every second by pressing on the up arrow
if (Input.GetKey (KeyCode.UpArrow)) {
transform.position += Vector3.forward * Time.deltaTime * Sp;
}
// this code make the object move back if the user press the down arrow in the keybord
if (Input.GetKey (KeyCode.DownArrow)) {
transform.position += Vector3.back * Time.deltaTime * Sp;
}
}
thaaaanks a lot, if i want to use force how can i use it. i used rigidbody to make my character collect money :)
Take a look at this page of the docs, you might want to create a test scene in your project to have a play around with physics and forces as there is a lot to learn :)
http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Answer by MrSoad · Dec 17, 2014 at 04:40 PM
The problem is that you are using the rigidbody and gravity is doing what it would in real life, the result being what you see in your pic above.
Do you really need to use a rigidbody and physics for this? You are moving your character by transform.Translate and transform.position which is not good if you do want to use physics, you should use forces instead.
If you do want to stick with physics then in your rigidbody settings look at "Constraints", now under "Freeze Rotation" click "X" and "Z". This will allow the character to rotate around the Y axis (rotate left and right) but not along the others(which would allow it to fall down).