Search Unity

Basic MOVE and JUMP script (Jump trouble)

Discussion in 'Scripting' started by SADOOO, Jan 2, 2017.

  1. SADOOO

    SADOOO

    Joined:
    Jul 9, 2015
    Posts:
    8
    Hello,

    I need help with fixing script, I simply need basic ASDW movement and one hit SPACE jump (forbidden jump while in air) with public floats so I can test it and set the right values. (need to jump around 110cm fast).

    What I have now work for movement but jump is more like jetpack, it does slowly speed upwards.

    I would be glad if somebody would be so kind and helped me with jump issue as I am not programmer, just need this for level design setting/browsing.

    Would prefer posting whole functioning code for the same reason.

    Thanks in advance.




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharacterController : MonoBehaviour {
    5.     private Rigidbody rg;
    6.     public float speed = 10.0F;
    7.     public float jumpspeed = 10.0F;
    8.    
    9.    
    10.     void Start () {
    11.         Cursor.lockState = CursorLockMode.Locked;
    12.         rg = GetComponent <Rigidbody> ();
    13.     }
    14.    
    15.    
    16.     void Update () {
    17.         float translation = Input.GetAxis ("Vertical") * speed;
    18.         float straffe = Input.GetAxis ("Horizontal") * speed;
    19.         translation *= Time.deltaTime;
    20.         straffe *= Time.deltaTime;
    21.        
    22.         transform.Translate (straffe, 0, translation);
    23.        
    24.         if (Input.GetKey (KeyCode.Space)) {
    25.             Vector3 atas = new Vector3 (0,100,0);
    26.             rg.AddForce(atas * speed);
    27.         }
    28.        
    29.         if (Input.GetKeyDown ("escape"))
    30.             Cursor.lockState = CursorLockMode.None;
    31.        
    32.        
    33.     }
    34.    
    35. }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    You need to test if the player is on the ground or not. There are plenty of ways to do this from the simplest but the worst being casting a ray downward for some length, and if it hits, you're on ground, to the best being a different collider that sends that analyzes the collisions he gets and if it's the ground he's colliding with, then it sets the players onGround variable to true
    However I'm NOT going to write you a code, because a.) this forum is not for that, if you want a complete code, go to the asset store or the paid section b.) There are plenty of tutorials online or even code snippets that work. Or if you really don't want to spend time on this, there is the built-in fps controller
     
    MarshianMan and TaleOf4Gamers like this.
  3. SADOOO

    SADOOO

    Joined:
    Jul 9, 2015
    Posts:
    8
    I understand your point of view, could you tell me where I can set built in fps controller? I was actually looking for something like that in the first place... I am using free version tho, is it there too?
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Yeah, if you right click in your assets folder, there is an import package menu and there in one of the packages (I can't remember which one though, maybe Character, look it up with google) then you import it, and you should be able to use it
     
    TaleOf4Gamers likes this.
  5. jmkdavidow

    jmkdavidow

    Joined:
    Jan 2, 2017
    Posts:
    5
    To check if the player is grounded do
    if (variable.isGrounded);
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Sorry, but what? What variable? what isGrounded? Why is there a semicolon at the end?
     
  7. MarshianMan

    MarshianMan

    Joined:
    Mar 27, 2015
    Posts:
    50
    Hello SADOOO,

    If you want the player to jump instantly rather than having a jetpack-esque motion, add ForceMode.Impulse to the rigidyboy.AddForce. Below is a copy of your script with the minor change, as well as a link to the Unity Docs for some extra information!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharacterController : MonoBehaviour {
    5.     private Rigidbody rg;
    6.     public float speed = 10.0F;
    7.     public float jumpspeed = 10.0F;
    8.  
    9.  
    10.     void Start () {
    11.         Cursor.lockState = CursorLockMode.Locked;
    12.         rg = GetComponent <Rigidbody> ();
    13.     }
    14.  
    15.  
    16.     void Update () {
    17.         float translation = Input.GetAxis ("Vertical") * speed;
    18.         float straffe = Input.GetAxis ("Horizontal") * speed;
    19.         translation *= Time.deltaTime;
    20.         straffe *= Time.deltaTime;
    21.    
    22.         transform.Translate (straffe, 0, translation);
    23.    
    24.         if (Input.GetKey (KeyCode.Space)) {
    25.             Vector3 atas = new Vector3 (0,100,0);
    26.             rg.AddForce(atas * speed, ForceMode.Impulse);
    27.         }
    28.    
    29.         if (Input.GetKeyDown ("escape"))
    30.             Cursor.lockState = CursorLockMode.None;
    31.    
    32.    
    33.     }
    34.  
    35. }
    https://docs.unity3d.com/ScriptReference/ForceMode.Impulse.html

    Hope this helps!
     
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    The only problem is that he's not checking if the player is on ground, so he flies up indefinitly
     
    MarshianMan likes this.
  9. MarshianMan

    MarshianMan

    Joined:
    Mar 27, 2015
    Posts:
    50
    Oh! I did not catch that, thank you. I also just realized the name of the script is "CharacterController" which should be changed as this will conflict with Unity's built-in Character Controller component, so he should also change the name of the script.

    Speaking of which, as long as he is using the default Character Controller he should just include this small bit of code.

    Code (CSharp):
    1. if (Input.GetKey (KeyCode.Space) && gameObject.GetComponent(CharacterController).isGrounded == true) {
    2.             Vector3 atas = new Vector3 (0,100,0);
    3.             rg.AddForce(atas * speed, ForceMode.Impulse);
    4.         }
    Otherwise, the method gorbit99 mentioned of using RayCast should be implemented.
     
    Last edited: Jan 4, 2017
  10. SADOOO

    SADOOO

    Joined:
    Jul 9, 2015
    Posts:
    8
    Thank you, first script burst worked, but kept jumping in the air, second says there is unexpected symbol "=" :( And yes, there is character controller on capsule.
     
    Last edited: Jan 4, 2017
  11. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Yeah, there are problems with that, sorry @TBranflakes, but the code is just wrong
    First of all, you wrote = instead of == in the if statement, secondly you don't even need that, you can leave it out because it is a boolean, you can just leave the check out
     
  12. MarshianMan

    MarshianMan

    Joined:
    Mar 27, 2015
    Posts:
    50
    It has been fixed, sorry. :)

    That's what happens when you copy and paste and don't check your work, haha, thank you @gorbit99. I like to keep it spelled out when dealing with people who've never coded before.
     
  13. SADOOO

    SADOOO

    Joined:
    Jul 9, 2015
    Posts:
    8
    Stil does not work tho... Changed name of public class and file too... Now it says errors need fixing without displaying any errors -.-

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Capsule : MonoBehaviour {
    5.     private Rigidbody rg;
    6.     public float speed = 10.0F;
    7.     public float jumpspeed = 10.0F;
    8.  
    9.  
    10.     void Start () {
    11.         Cursor.lockState = CursorLockMode.Locked;
    12.         rg = GetComponent <Rigidbody> ();
    13.     }
    14.  
    15.  
    16.     void Update () {
    17.         float translation = Input.GetAxis ("Vertical") * speed;
    18.         float straffe = Input.GetAxis ("Horizontal") * speed;
    19.         translation *= Time.deltaTime;
    20.         straffe *= Time.deltaTime;
    21.      
    22.         transform.Translate (straffe, 0, translation);
    23.      
    24.         if (Input.GetKey (KeyCode.Space) && gameObject.GetComponent(CharacterController).isGrounded == true)
    25.             Vector3 atas = new Vector3 (0,100,0);
    26.             rg.AddForce(atas * speed, ForceMode.Impulse);
    27.         }
    28.      
    29.         if (Input.GetKeyDown ("escape"))
    30.             Cursor.lockState = CursorLockMode.None;
    31. }
    32.      
    33.     }
    34.  
    35.  
     
  14. MarshianMan

    MarshianMan

    Joined:
    Mar 27, 2015
    Posts:
    50
    Check the brackets. It appears that the opening bracket for the if-statement to check if you've pressed the Space button is missing.
     
  15. SADOOO

    SADOOO

    Joined:
    Jul 9, 2015
    Posts:
    8
    Where do I do that and how do I fix it when it is missing please... But problem is I cant even run the game, as it say it has errors...
     
  16. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,768
    Go through your code, and for each opening bracket, find the closing bracket. Make sure your code is within the correct pair and that you don't have any extras.

    This is something you need to learn to do on your own.
     
    MarshianMan likes this.
  17. SADOOO

    SADOOO

    Joined:
    Jul 9, 2015
    Posts:
    8
    In the end I somehow bustled it and got it "working" how I want even tho I have some crazy values to achieve realistic feeling... Gravity -98...Drag1, weight 80... Jump 3500 burst... Got one question tho, How to I display value for jump "3500" and why does not code values change when I change them in unity public float?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Capsule : MonoBehaviour {
    5.     private Rigidbody rb;
    6.     public float speed = 100.0F;
    7.     public float raycastLength = 0.52F;
    8.  
    9.     void Start () {
    10.         Cursor.lockState = CursorLockMode.Locked;
    11.         rb = GetComponent <Rigidbody> ();
    12.  
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.     float translation = Input.GetAxis ("Vertical") * speed;
    18.         float straffe = Input.GetAxis ("Horizontal") * speed;
    19.         translation *= Time.deltaTime;
    20.         straffe *= Time.deltaTime;
    21.      
    22.         transform.Translate (straffe, 0, translation);
    23.      
    24.         if(Physics.Raycast(transform.position, -transform.up, raycastLength))
    25.      
    26.             if(Input.GetKeyDown(KeyCode.Space))
    27.             {
    28.                 Vector3 atas = new Vector3 (0,3500,0);
    29.             rb.AddForce(atas, ForceMode.Impulse);
    30.             }
    31.  
    32.  
    33.         if (Input.GetKeyDown ("escape"))
    34.             Cursor.lockState = CursorLockMode.None;
    35.     }
    36.  
    37.  
    38. }
    39.  
    40.  
     
    Last edited: Jan 5, 2017
  18. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,768
    If you assign a value to the public variable in the code, it only represents the "default" value for that variable. When you make a new component of that type, it will get that value. Once you change it in the inspector, that new value overrides the default even if you change it in the script.
     
  19. SADOOO

    SADOOO

    Joined:
    Jul 9, 2015
    Posts:
    8
    So when I create gravity float for capsule, it will modify gravity value affecting capsule while other objects follow system gravity or does it override overall gravity settings?
     
  20. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,768
    What do you mean by 'create gravity float'?

    If you assign Physics.Gravity, it will affect overall gravity settings for all objects.

    For 3D physics, the only way to set gravity for a single object is to disable Rigidbody.useGravity, and then add your own gravity using FixedUpdate, where you would use AddForce to add a gravity force of your choosing.
     
  21. SADOOO

    SADOOO

    Joined:
    Jul 9, 2015
    Posts:
    8
    Ok, thx.
     
  22. PRABHBIR123

    PRABHBIR123

    Joined:
    Apr 9, 2017
    Posts:
    72
    Hey @SADOOO check out this tutorial.
     
unityunity