scripting problem >:(

How would I add a jump / double jump to my script and someone please answer my question :slight_smile:

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

    Animator anim;

    bool grounded = false;
    public Transform groundCheck;
    float groundRadius = 0.2f;
    public LayerMask whatIsGround;


    // Use this for initialization
    void Start () {
   
        anim = GetComponent<Animator> ();
    }

    void FixedUpdate (){

        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
        anim.SetBool ("Ground", grounded);

    }

    // Update is called once per frame
    void Update () {

        float input_x = Input.GetAxisRaw ("Horizontal");
        float input_z = Input.GetAxisRaw ("Vertical");

        bool IsWalking = (Mathf.Abs (input_x) + Mathf.Abs (input_z)) >0;
        anim.SetBool ("IsWalking", IsWalking);
        if (IsWalking)
        {
            anim.SetFloat ("x", input_x);
            anim.SetFloat ("z", input_z);

            transform.position += new Vector3(input_x, 0f, input_z).normalized * Time.deltaTime;
            if (grounded && Input.GetKeyDown (KeyCode.Space))
            {
                anim.SetBool ("Ground",false);
            }
        }
    }
}

What do I do to add a jump / double jump to my script?

I don’t recall seeing anywhere in the Terms of Service where we’re required to answer questions posed on these forums.

Is it unfair that people are skipping over your questions? Or is it your attitude that is encouraging it? Because with the statements you’ve made in that post, I don’t want to assist you in the slightest.

3 Likes

Sorry just real frustrated right now :frowning:

Frustration like this will result in no responses, but shhhhhure why not I’ll look over the question give me a second.

Buuut before that let me help you a little more for later questions, try asking questions in this format, it really helps if you’re not good at asking questions.

Problem : State the problem - I’ve determined you’re having issues figuring out how to add jumping to your game. What part are you having issues with, the input? or the actual jumping? Without that we can’t really tell you how to fix what you have and if we do, there will likely be 20 different responses with different ways and you will likely get confused.

What would you like to do with the code: Tell us what you’re trying to accomplish and whether there are any constraints on what can or cant be used to accomplish it.

Give the code: You got this one done alright.

Lastly unrelated, but are you doing this as part of a tutorial? or just learning? or for school?

BUT to answer, lets see… For a jump you’re on the right track you want an input.getkeydown(KeyCode.Space), which will basically tell the game frame that you’ve hit the space bar. As for jumping there are several ways to go about doing that,

Go to API Page, Look up Transform.Translate(); It takes a vector3, which controls 3 axes.

^For that Y Value if you just usea value and dont use any Mathf class functions to smooth the movement its going to look pretty bad, so I’d suggest looking into Mathf.Lerp and other functions that could be useful in smoothing that jump.

The second method is probably more practical. If you have a rigidbody attached to the player you can add a force to it,

Go to API page, Look up Rigidbody.AddForce(); It takes a vector3 value as a parameter which again control the axes.

And obviously, you’d want to check for grounded to ensure that your character is on the ground. To do a double jump just have a few if statements that toggles a seperate boolean values so that checks for,

  1. if statement 1 - bool value - true if grounded, false if not grounded.
    a. whether you’re grounded - Allows for you to jump.
  2. if statement 2 - bool value - false if jumped once true if jumped twice.
    b. Once you’ve jumped, whether you’ve jumped twice.

Then you just reset the bool value for double jump back to false when you’re re grounded.

tutorial and yes a project

School project? If thats the case, me telling you does you nothing, so you’re going to have to work for this one. I’ve given some starting points above.

no a project of my own not a school project lol :slight_smile: i’m not even taking a class for unity lol

Well in any case learning to use the API pages for learning is a valuable skill with any program, so hug your new friend the API page and have a sleepover, you can stay up as late as you want reading through it. For a new programmer I’d suggest learning all you can about TRANSFORMS, as well as VECTOR3’s, and QUATERNION’S. Additionally, things like RIGIDBODY can be useful in some scenarios, in fact most scenarios. All of these are classes that can be found here, with all of their functions listed.

Here is a link to Rigidbody, get reading, and feel your brain grow.

thanks i’ll just look into this then :slight_smile: