Search Unity

Adding Gravity - Unity C# Basic Code

Discussion in 'Scripting' started by TheRexenor, Oct 8, 2015.

Thread Status:
Not open for further replies.
  1. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    I updated my C# code based on this tutorial in order to add gravity to my character controller that is adjustable but gravity doesn't seem to affect my space vessel :(

    Can anyone point out the mistake on my code?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(CharacterController))]
    4. public class SpaceNavigation : MonoBehaviour
    5.  
    6. {
    7.     public float MoveSpeed;
    8.     public float RotationSpeed;
    9.     public float gravity = 9.8f;
    10.     CharacterController cc;
    11.     Vector3 currentMovement;
    12.     // Use this for initialization
    13.     void Start ()
    14.      
    15.     {
    16.         cc = GetComponent<CharacterController>();
    17.     }
    18.     // Update is called once per frame
    19.     void Update () {
    20.      
    21.         Vector3 foward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * MoveSpeed;
    22.         transform.Rotate(new Vector3(0,Input.GetAxis("Horizontal") * RotationSpeed * Time.deltaTime,0));
    23.         cc.Move(foward * Time.deltaTime);
    24.      
    25.         currentMovement = new Vector3(0, currentMovement.y, Input.GetAxis("Vertical") * MoveSpeed);
    26.         currentMovement = transform.rotation * currentMovement;
    27.         if (!cc.isGrounded)
    28.             currentMovement -= new Vector3 (0, gravity * Time.deltaTime, 0);
    29.         //cc.SimpleMove(Physics.gravity);
    30.     }
    31. }
     

    Attached Files:

    EliasMasche likes this.
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    I don't see in anywhere that you actually make the gravity variable useful. You are setting it in some ways, but not putting it into any other place than it was set originally. Since you are passing forward variable into Move function, you need to decrease that vector's y value by the amount of gravity. You want to apply gravity when your object is on air, then you go:
    Code (CSharp):
    1. if(!cc.isGrounded)
    2.     forward.y -= gravity * Time.deltaTime;
    3.  
    4. cc.Move(forward * Time.deltaTime);
     
    TheRexenor likes this.
  3. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    Hi lineupthesky, thanks for replying!!! Just to let you know I'm not in to programming, all I want is to attach a character controller script to my 3d graphic that lets you move, rotate and have adjustable gravity!

    I'll try to implement what you just send me and see if that works for me!!!
     
  4. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
  5. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class junk : MonoBehaviour {
    5.  
    6.     private CharacterController cc;
    7.     public float MoveSpeed;
    8.     public float RotationSpeed;
    9.     public float gravity;
    10.     // Use this for initialization
    11.     void Start () {
    12.         cc = GetComponent<CharacterController>();
    13.     }
    14.    
    15.     void Update () {
    16.        
    17.         Vector3 foward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * MoveSpeed;
    18.         transform.Rotate(new Vector3(0,Input.GetAxis("Horizontal") *RotationSpeed* Time.deltaTime,0));
    19.         if(!cc.isGrounded)
    20.             foward.y -= gravity * Time.deltaTime;
    21.  
    22.         cc.Move(foward * Time.deltaTime);
    23.  
    24.  
    25.     }
    26. }
    27.  
    When I add this code to a cube, and adjust the variables in the inspector, everything works fine.
     
  6. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Something may be easier than using a character controller.
    Espically for a spaceship.

    Why not try going to a Rigidbody? Much more better to use and actually works with Physics. a Character Controller doesn't - thus won't move or push anything.
    Then at least you can do just do something like

    Code (CSharp):
    1. public float myGravity;
    2. private Rigidbody myRigidbody;
    3.  
    4. void Start(){
    5. myRigidbody = GetComponent<Rigidbody>();
    6. }
    7.  
    8. void FixedUpdate(){
    9. myRigidbody.AddRelativeForce(Vector3.down * myGravity);
    10.  
    11. // Now you can add the rest of your Character Controller //controls here, however be sure to change them to AddForce //or AddRelativeForce (The former is the global world axis, the //latter is the local axis of the object). So I tend to always use //AddRelativeForce
    12.  
    13. }
     
  7. TheRexenor

    TheRexenor

    Joined:
    Jun 12, 2015
    Posts:
    23
    lineupthesky you are awesome, that works great!!! N1warhead thanks for replying!!!

    Basically, i'm planning to create a situation where there is zero gravity or a boost function that affects the vessels gravity! So I think that a Character Controller is necessary to control these values, correct? Is it possible to affect these values with Rigidbody?

    What is the standard/best way to tackle a space racer idea?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'd go with rigid bodies. Character controllers are specifically designed to control characters walking on the ground in a FPS type game. They do weird things to physics to make the character more responsive. They are not really directly applicable to space ships.
     
  9. JayMakes

    JayMakes

    Joined:
    Nov 26, 2020
    Posts:
    1
    hippity hoppity your code is now my property
     
    wknab83 likes this.
  10. Partially_3

    Partially_3

    Joined:
    Dec 26, 2020
    Posts:
    1
    none of this is working:(
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Don't reply to a five-year-old thread with a useless zero-content one-sentence response.

    Instead, write your own new post with ALL of the information necessary for someone to understand you.

    When you do, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    If you post code, please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    wknab83 likes this.
Thread Status:
Not open for further replies.