#Input system
1 messages Ā· Page 1 of 1 (latest)
Yeah, I just gotta make sure this works, so I don't mislead you
So, are you familiar with variables like floats?
Asking so I know how to explain this.
i know them a bit
floats are decimals, variables are things that can change
i think
Yeah, that's right.
like private bool isJumping;
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()
{
}
}
Yes
You'll need to create a float in the header. We'll name it jumpPower
Alright
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()
{
}
}
K
Unity has this "function" called Time.deltaTime
This is how we'll add the power in
Alright
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
}
}
}
In this, we're adding the product of someNumber and Time.deltaTime to jumpPower
haha, you're right. I forgot that part. Hold on
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?
Now, someNumber can be any number you want.
You would change it to change the speed of jumpPower changing
Ah
So, jumpPower += 2f * Time.deltaTime would make jumpPower go up at 2 per second, instead of 1 per second
yes
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?
Could you send me a screenshot of that script, please?
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
Well, Update() needs to be void Update() instead. Otherwise, the compiler won't recognize it.
Ah
When you change that, does anything change?
I have another update script in my c# file I have to move it there first
I donāt notice a difference
Alright. So, as of right now, nothing should happen after you let go of space, unless I missed something in this script
I will make the multiplier bigger
So, the character is physically jumping in the scene?
Itās the same if hold and if I donāt
Yes
Then, where is Jump() being called from? I don't see anything calling Jump() from the screen shot you sent.
And if I change the multiplier to 100 I definitely jump higher, but itās not different between holding and not holding
Lemme check
Here
I sent my jump script
Earlier
No, that would be the Jump() method itself. I'm looking for where Jump() is being called.
Can I click on references to see where it is called?
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
}
}
}
Yeah, I think you shift + click the refernce
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"
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?
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
Yeah, I could see how that would get annoying
It would definitely work for something that you ācharge upā but idk about jumping
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?
I think so, I didnāt write I just edited it to my needs
Iām using rigid body
Alright. You could apply a force the whole way up instead of a single impulse
Could you paste your whole script here, if you don't mind? https://pastebin.com/
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
So ignoring syntax, if space key pressed count time pressed and add/multiply that value to force?
Sure
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Well, not exactly, anymore. My bad. I should've asked you to clarify what kind of jumping you wanted. Disregard the other things I said. Because I can't think of anything else, I think applying force all the way up would be the way to go.