#Jump is not working

1 messages · Page 1 of 1 (latest)

nimble cliff
#

I wrote every line of code like from https://youtu.be/_QajrabyTJc this video
I`ve done everything what neede to do but space button not reading in the game window or code have some issues i have no ide how to fix it

velvet spireBOT
#

It's hard to answer a programming question without code

Resolving a bug is almost impossible when the question doesn't include any of the buggy code. In order to help fix the problem, answerers are going to have to see what the code is.
Source: https://idownvotedbecau.se/nocode

Please isolate the problematic code and send it as a codeblock. If you don't know how to send a codeblock, type []cb

nimble cliff
#

[]cb

velvet spireBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

nimble cliff
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public CharacterController controller;
    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;
    Vector3 velocity;
    bool isGrounded;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groudMask;

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groudMask);

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if (isGrounded && velocity.y < 0f)
        {
            velocity.y = -2f;
        }


        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * 2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}
nimble cliff
#

HEEEELP

#

@timber shale

#

Why bot is not answering

timber shale
#

What would the bot even answer?

nimble cliff
#

he would help?

timber shale
#

No, there's no magic help bot, you gotta wait till someone helps.

nimble cliff
#

aaa

#

and why there bot

#

if it hasnt any help?

timber shale
#

Because I executed the command but removed the command message.

near hamlet
nimble cliff
#

i will try to debug

nimble cliff
#

@near hamlet

near hamlet
#

Yes?

nimble cliff
#

?

near hamlet
#

Well yes

nimble cliff
#

@near hamlet i need to check console of unity?

near hamlet
#

Exactly. From the results, you might get a better understanding of where the actual problem lies in your code.

nimble cliff
#

object isnt grounded even if it grounded

#

xD

#

How i can fix it?

nimble cliff
near hamlet
#

Somethings wrong with either your groundCheck, **groundDistance **or your ground does not have the groundMask / The **groundMask **isn't assigned properly in the inspector.

nimble cliff
#

what a hell

#

i gave to terrain Ground group and know i am flying

near hamlet
#

Now you're flying?

nimble cliff
#

yea)

near hamlet
#

As in, you're flying after one jump, or you can jump all the time?

nimble cliff
#

after 1 jump

#

there i pressed SPACE at the high height and i changed Y vector but it doesnot changed after Jump

nimble cliff
near hamlet
#

It isn't assigned to your ground? Perhaps?

nimble cliff
#

??

#

engine seeing that groundCheck and groundMask has touched each others?

#

I think i should to send this project file to you

near hamlet
#

I can't look at it now though

#

Send a video instead

nimble cliff
#

it have big size for discrod

#

so

#

what do you think? in what thing is problem? code? engine? or mistake on values

near hamlet
#

Problem with your setup in engine most likely

#

Show me your grouneCheck, movement script component and ground object

#

With images

lofty saffron
#

Hi! Buddy, you miscalculate the velocity. Here:

 velocity.y = Mathf.Sqrt(jumpHeight * 2f * gravity);

You need to change to :

  velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);

Because I do believe that you want to player to fall down not to climb up. Check and see if this helps

nimble cliff
#

@lofty saffron Thank you

lavish tendon
#

It should be -2

#

because gravity is also negative

#

Ah you were making the correction

#

Well, what you said about "falling down not up" is not true. Having it as 2 wouldn't make it go up. It'd result in an error since Mathf.Sqrt does not work with complex numbers