- Home /
Jump axis from 0 to max
So I have a code that input jump value using the Jump Axis. I want that value to go from 0 to max every time I press space. but what happens is when I press slightly the jump axis doesn't go to full value. which causes my jump to be less higher. Here's my code.
public class TPcontrollertest : MonoBehaviour
{
public float movementSpeed = 6f;
public CharacterController controller;
private Vector3 direction = Vector3.zero;
private Vector3 move;
private Animator anim;
private Rigidbody rbody;
float horizontal;
float vertical;
float jump;
void Awake()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody>();
}
void Update()
{
anim.SetFloat("move", direction.z);
anim.SetFloat("turn", direction.x);
//movement
jump = Input.GetAxisRaw("Jump");
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
direction = new Vector3(horizontal, jump, vertical).normalized;
move = transform.right * horizontal + transform.forward * vertical + transform.up * jump;
if (direction.magnitude >= 1.0f)
{
direction = direction.normalized;
}
controller.Move(move * movementSpeed * Time.deltaTime);
Jump();
}
void Jump()
{
if (Input.GetButtonDown("Jump"))
{
anim.SetTrigger("jump");
}
}
}
this code includes the movement as well but the part I want to modify is the jump axis going from 0 to max when I press space no matter how long I press it.
Answer by jamesvhyde · Jul 25, 2020 at 10:32 PM
Add a line of code after you get the axis value:
jump = Input.GetAxisRaw("Jump");
if (jump > 0f) jump = 1f;
this is the axis I'm using, I want it to uniformly translate from 0 to max every single press on space, wether it's a long press or not. the line you provided didn't change anything :(.
I'm pretty sure that a keypress, read as an axis, only has two values--0 and 1. I'm not sure what you mean by "uniformly translate from 0 to max every single press." I don't think an axis will work for you for this. Jumping requires more logic. If you are using physics, a jump applies an upward force once, and gravity brings it back down. If you're not using physics (rigid bodies), then you will have to implement the up-and-down motion yourself. You will have to keep track of how long the character has been jumping, and turn it around when the maximum height has been reached.
I tried doing what you said. but I ran into another error which I really don't know what is happening to be honest. $$anonymous$$y CharacterController doesn't detect ground by all means though it detected collision on the sides. also the rigidbody doesn't apply force as well. it's like the physics is not working.
Your answer
Follow this Question
Related Questions
Why are my animations not working correctly? Thanks! 0 Answers
2D Character Animation - even starting if I'm not pressing A or D 2 Answers
Character will not go from Ide to Walk 1 Answer
My Animation Is Stuck On A Frame 0 Answers
How to make character controller forward jump a certain distance? 1 Answer