#Input system

1 messages Ā· Page 1 of 1 (latest)

verbal heath
#

Alright, give me a sec

ashen prawn
#

ok

#

privvet are you there

#

i dont wanna be impatient lol

verbal heath
#

Yeah, I just gotta make sure this works, so I don't mislead you

ashen prawn
#

ok ty

#

i really apreciate this

verbal heath
#

So, are you familiar with variables like floats?

#

Asking so I know how to explain this.

ashen prawn
#

i know them a bit

#

floats are decimals, variables are things that can change

#

i think

verbal heath
#

Yeah, that's right.

ashen prawn
#

like private bool isJumping;

verbal heath
#

So, when you create a c# script in Unity, the script starts off looking something like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JumpScript : MonoBehaviour
{
  Start()
  {
    
  }

  Update()
  {

  }
}
ashen prawn
#

Yes

verbal heath
#

You'll need to create a float in the header. We'll name it jumpPower

ashen prawn
#

Alright

verbal heath
#

So, when you create a c# script in Unity, the script starts off looking like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JumpScript : MonoBehaviour
{
  //THE HEADER IS IN THIS SPACE RIGHT HERE
  public float jumpPower;
  Start()
  {
    
  }

  Update()
  {

  }
}
ashen prawn
#

K

verbal heath
#

Unity has this "function" called Time.deltaTime

ashen prawn
#

I think I got that already

#

For jump power

verbal heath
#

This is how we'll add the power in

ashen prawn
#

Alright

verbal heath
#

So, when you create a c# script in Unity, the script starts off looking like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JumpScript : MonoBehaviour
{
  Start()
  {
    
  }

  Update()
  {
    if (Input.GetKey(KeyCode.Space))
    {
      jumpPower += someNumber * Time.deltaTime
    }
  }
}
ashen prawn
#

How does deltatime know that it’s space key?

#

Or is that not defined yet

verbal heath
#

In this, we're adding the product of someNumber and Time.deltaTime to jumpPower

#

haha, you're right. I forgot that part. Hold on

ashen prawn
#

Ah

#

Isn’t getinput or something

verbal heath
#

Yes

#

Input.GetKey(KeyCode.Space)

#

While it's in Update(), it's checking if the user is holding down the space bar

#

So, IF the user is holding space down, add the product of someNumber and Time.deltaTime to jumpPower

#

EVERY SECOND

#

Following?

ashen prawn
#

Yes

#

Ur a good teacher man

verbal heath
#

Now, someNumber can be any number you want.

#

You would change it to change the speed of jumpPower changing

ashen prawn
#

Ah

verbal heath
#

So, jumpPower += 2f * Time.deltaTime would make jumpPower go up at 2 per second, instead of 1 per second

ashen prawn
#

yes

verbal heath
#

jumpPower += 3f * Time.deltaTime would make it go up at 3 per second

#

Now that just makes the number go up. That doesn't really help us.

#

How are you making the jumping happen?

#

Are you using rigidbody.AddForce? Or have you started programming the actual jumping yet?

ashen prawn
#

I we could input jump power to my jumping velocity script

#

I’m trying that rn

verbal heath
#

Could you send me a screenshot of that script, please?

ashen prawn
#

Sure

#

jump force is the name i used

#

It doesn’t like update

#
 private void Jump()
    {
        if (grounded)
        {
            readyToJump = false;

            //Add jump forces
            rb.AddForce(Vector2.up * jumpForce * 1.5f);
            rb.AddForce(normalVector * jumpForce * 0.5f);

            //If jumping while falling, reset y velocity.
            Vector3 vel = rb.velocity;
            if (rb.velocity.y < 0.5f)
                rb.velocity = new Vector3(vel.x, 0, vel.z);
            else if (rb.velocity.y > 0)
                rb.velocity = new Vector3(vel.x, vel.y / 2, vel.z);

            Invoke(nameof(ResetJump), jumpCooldown);
        }
        if (!grounded)
        {
            readyToJump = false;

            //Add jump forces
            rb.AddForce(orientation.forward * jumpForce * 1f);
            rb.AddForce(Vector2.up * jumpForce * 1.5f);
            rb.AddForce(normalVector * jumpForce * 0.5f);

            //Reset Velocity
            rb.velocity = Vector3.zero;

            //Disable dashForceCounter if doublejumping while dashing
            allowDashForceCounter = false;

            Invoke(nameof(ResetJump), jumpCooldown);
        }```
#

this is jump script

verbal heath
#

Well, Update() needs to be void Update() instead. Otherwise, the compiler won't recognize it.

ashen prawn
#

Ah

verbal heath
#

When you change that, does anything change?

ashen prawn
#

Yes

#

It makes it blue

verbal heath
#

Alright. Run the code in Unity and try jumping. What happens then?

#

Oh wait

ashen prawn
#

I have another update script in my c# file I have to move it there first

#

I don’t notice a difference

verbal heath
#

Alright. So, as of right now, nothing should happen after you let go of space, unless I missed something in this script

ashen prawn
#

I will make the multiplier bigger

verbal heath
#

Oh ok

#

No, not yet

ashen prawn
#

Oh?

#

I can still jump

#

I just don’t notice if it’s higher than before

verbal heath
#

So, the character is physically jumping in the scene?

ashen prawn
#

It’s the same if hold and if I don’t

verbal heath
#

Then, where is Jump() being called from? I don't see anything calling Jump() from the screen shot you sent.

ashen prawn
#

And if I change the multiplier to 100 I definitely jump higher, but it’s not different between holding and not holding

ashen prawn
#

I sent my jump script

#

Earlier

verbal heath
#

No, that would be the Jump() method itself. I'm looking for where Jump() is being called.

ashen prawn
#

Can I click on references to see where it is called?

verbal heath
#

For example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JumpScript : MonoBehaviour
{
  Start()
  {
    
  }

  public void Jump()
  {
    //Do stuff
  }

  Update()
  {
    if (Input.GetKey(KeyCode.Space))
    {
      jumpPower += someNumber * Time.deltaTime
    }
    if (Input.GetKey(KeyCode.E))
    {
      Jump(); //JUMP() IS BEING CALLED HERE
    }
  }
}
verbal heath
ashen prawn
#

Ok then:

#
if (Input.GetButtonDown("Jump") && !grounded && doubleJumpsLeft >= 1)
        {
            Jump();
            doubleJumpsLeft--;
        }```
#

i have double jumps

#

the other place it is called is for my eventual addition of jetpacks

#
        if (readyToJump && jumping && grounded && !rocketActive) Jump();```
#

nvm

#

this is also for jumping if im not mistaken

#

because ! means "not"

verbal heath
#

Oh wait

#

Did you want the player to jump after they've let go of space, or did you want them to jump immediately and the duration of the jump is how long they're holding down space?

ashen prawn
#

prolly how long they are holding space because making it jump after seems kinda clunky

#

like in rocket league if you tap jump, it jumps immediately depending on how long you pressed

verbal heath
#

Ah, ok

#

Hold on. Let me rethink this

ashen prawn
#

to me this seems harder

#

but i just feel it would be annoying waiting for it to jump

verbal heath
#

Yeah, I could see how that would get annoying

ashen prawn
#

It would definitely work for something that you ā€œcharge upā€ but idk about jumping

verbal heath
#

Oh ok. I think I got it

#

Does your character controller script have its own way to do gravity?

#

Or are you using a rigidbody?

ashen prawn
#

I think so, I didn’t write I just edited it to my needs

ashen prawn
verbal heath
#

Alright. You could apply a force the whole way up instead of a single impulse

ashen prawn
#

So ignoring syntax, if space key pressed count time pressed and add/multiply that value to force?

ashen prawn
verbal heath
ashen prawn
#

Wdym by applying the force all the way up?

#

Same force, but then stopping it instead of a different value based on duration

#

That is my interpretation

verbal heath
#

Like, while the player is pressing space, apply 100 force every second, but for each second that passes, apply less and less force

#

Do you mind going into a call so I can screen share the code while I edit it so you can see? Just so I can explain this better.