Basic MOVE and JUMP script (Jump trouble)

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.

using UnityEngine;
using System.Collections;

public class CharacterController : MonoBehaviour {
    private Rigidbody rg;
    public float speed = 10.0F;
    public float jumpspeed = 10.0F;
   
   
    void Start () {
        Cursor.lockState = CursorLockMode.Locked;
        rg = GetComponent <Rigidbody> ();
    }
   
   
    void Update () {
        float translation = Input.GetAxis ("Vertical") * speed;
        float straffe = Input.GetAxis ("Horizontal") * speed;
        translation *= Time.deltaTime;
        straffe *= Time.deltaTime;
       
        transform.Translate (straffe, 0, translation);
       
        if (Input.GetKey (KeyCode.Space)) {
            Vector3 atas = new Vector3 (0,100,0);
            rg.AddForce(atas * speed);
        }
       
        if (Input.GetKeyDown ("escape"))
            Cursor.lockState = CursorLockMode.None;
       
       
    }
   
}

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

2 Likes

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?

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

1 Like

To check if the player is grounded do
if (variable.isGrounded);

Sorry, but what? What variable? what isGrounded? Why is there a semicolon at the end?

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!

using UnityEngine;
using System.Collections;

public class CharacterController : MonoBehaviour {
    private Rigidbody rg;
    public float speed = 10.0F;
    public float jumpspeed = 10.0F;


    void Start () {
        Cursor.lockState = CursorLockMode.Locked;
        rg = GetComponent <Rigidbody> ();
    }


    void Update () {
        float translation = Input.GetAxis ("Vertical") * speed;
        float straffe = Input.GetAxis ("Horizontal") * speed;
        translation *= Time.deltaTime;
        straffe *= Time.deltaTime;
    
        transform.Translate (straffe, 0, translation);
    
        if (Input.GetKey (KeyCode.Space)) {
            Vector3 atas = new Vector3 (0,100,0);
            rg.AddForce(atas * speed, ForceMode.Impulse);
        }
    
        if (Input.GetKeyDown ("escape"))
            Cursor.lockState = CursorLockMode.None;
    
    
    }

}

https://docs.unity3d.com/ScriptReference/ForceMode.Impulse.html

Hope this helps!

The only problem is that he’s not checking if the player is on ground, so he flies up indefinitly

1 Like

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.

if (Input.GetKey (KeyCode.Space) && gameObject.GetComponent(CharacterController).isGrounded == true) {
            Vector3 atas = new Vector3 (0,100,0);
            rg.AddForce(atas * speed, ForceMode.Impulse);
        }

Otherwise, the method gorbit99 mentioned of using RayCast should be implemented.

Thank you, first script burst worked, but kept jumping in the air, second says there is unexpected symbol ā€œ=ā€ :frowning: And yes, there is character controller on capsule.

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

It has been fixed, sorry. :slight_smile:

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.

Stil does not work tho… Changed name of public class and file too… Now it says errors need fixing without displaying any errors -.-

using UnityEngine;
using System.Collections;

public class Capsule : MonoBehaviour {
    private Rigidbody rg;
    public float speed = 10.0F;
    public float jumpspeed = 10.0F;
  
  
    void Start () {
        Cursor.lockState = CursorLockMode.Locked;
        rg = GetComponent <Rigidbody> ();
    }
  
  
    void Update () {
        float translation = Input.GetAxis ("Vertical") * speed;
        float straffe = Input.GetAxis ("Horizontal") * speed;
        translation *= Time.deltaTime;
        straffe *= Time.deltaTime;
      
        transform.Translate (straffe, 0, translation);
      
        if (Input.GetKey (KeyCode.Space) && gameObject.GetComponent(CharacterController).isGrounded == true)
            Vector3 atas = new Vector3 (0,100,0);
            rg.AddForce(atas * speed, ForceMode.Impulse);
        }
      
        if (Input.GetKeyDown ("escape"))
            Cursor.lockState = CursorLockMode.None;
}
      
    }

Check the brackets. It appears that the opening bracket for the if-statement to check if you’ve pressed the Space button is missing.

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…

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.

2 Likes

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?

using UnityEngine;
using System.Collections;

public class Capsule : MonoBehaviour {
    private Rigidbody rb;
    public float speed = 100.0F;
    public float raycastLength = 0.52F;

    void Start () {
        Cursor.lockState = CursorLockMode.Locked;
        rb = GetComponent <Rigidbody> ();

    }

    void Update ()
    {
    float translation = Input.GetAxis ("Vertical") * speed;
        float straffe = Input.GetAxis ("Horizontal") * speed;
        translation *= Time.deltaTime;
        straffe *= Time.deltaTime;
      
        transform.Translate (straffe, 0, translation);
      
        if(Physics.Raycast(transform.position, -transform.up, raycastLength))
      
            if(Input.GetKeyDown(KeyCode.Space))
            {
                Vector3 atas = new Vector3 (0,3500,0);
            rb.AddForce(atas, ForceMode.Impulse);
            }


        if (Input.GetKeyDown ("escape"))
            Cursor.lockState = CursorLockMode.None;
    }
  

}

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.

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?

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.