Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to ad gravity on my character controller script?

Discussion in 'Scripting' started by TheRexenor, Jun 13, 2015.

  1. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Hi there,

    I'm new to Unity 5 and ain't a programmer. I just found a tutorial on how to create a character controller and now I would like to implement gravity to my spaceship. Is there a simple line of code that could bring that result?

    Thanks!!!
    Character Controller script found on Youtube:

    using UnityEngine;
    using System.Collections;

    [RequireComponent(typeof(CharacterController))]

    public class SpaceNavigation : MonoBehaviour

    {
    public float MoveSpeed;
    public float RotationSpeed;
    CharacterController cc;

    // Use this for initialization
    void Start ()

    {
    cc = GetComponent<CharacterController>();
    }
    // Update is called once per frame
    void Update () {

    Vector3 foward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * MoveSpeed;
    transform.Rotate(new Vector3(0,Input.GetAxis("Horizontal") * RotationSpeed * Time.deltaTime,0));
    cc.Move(foward * Time.deltaTime);
    cc.SimpleMove(Physics.gravity);
    }
    }
    "
     
  2. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Have you tried making sure that your character has a rigidbody component attached and then clicking the gravity checkbox?

    Also, when posting code please be aware that you can use the tags [.code][./code] (without the periods) before and after your code snippet. It makes it much easier to read the code. Anyway, I hope this helps! good luck!
     
  3. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Hi topherbwell, Thank you very much for the response!!!

    Yes, but I don't see much difference. The spaceship keeps on floating in mid air when it doesn't collides with the ground until it eventually hits the ground (which takes about 20") I have also tried messing with the values Mass,Drag and ticked Gravity but oddly they don't seem to affect the ship's behavior.
     
  4. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This question and answer should clear it up. It's related to the Character Controller itself (the Unity one), not the Rigidbody.
     
  6. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Guys thanks again for responding!!! I just found what the issue was!!! I used real scale measurements in Max for my imported objects in Unity (track and spaceship) and they where huge. It was impossible to spot the gravity physics that where going on. However, I still need to adjust the gravity of my ship when that is off the ground; with C# code

    So the next question is how small should things be in a game world in relation to the real world in order to work properly?
     
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    It depends on the game, but if you're a grounded character trying to simulate real life, 1 unit = 1 metre is generally advised. If your game takes place in space though, that's going to be a ridiculous scale.
     
  8. reizibarrientos

    reizibarrientos

    Joined:
    Nov 10, 2016
    Posts:
    2
    This is very very 100% helpful