Disabling jump for 0.25 sec after you hit the ground after jumping?
It's a minor thing but I wouldn't want the character to stand on the spot and jump endlessly, a pause in between the jumps would be nice. I thought I might be able to disable the jump using a coroutine but it doesn't seem to work. Any more experienced programmer see the obvious problem?
I guess I might also ask why the sound of the character jumping sometimes happens twice. I guess when he is jumping he is touching the ground for more than one frame. Not sure how to solve this, I also find it odd that I can't multiply rotation by delta time. Are there any obvious solutions to any of these issues? :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Character : MonoBehaviour {
//Todo
//•How to multiply rotation by deltaTime?
//•Why can't I give the jump a float?
//•The jump sound is played twice?
Transform playerTransform;
Rigidbody playerRigidBody;
public AudioSource Jump;
public Text Gametimer;
public float characterFSpeed = 4.5f;
public float characterBSpeed = 2f;
public float jumpHeight = 150f;
public bool touchesGround;
public bool inTheAir;
public bool jumpEnabled;
void Start () {
playerTransform = GetComponent<Transform>();
playerRigidBody = GetComponent<Rigidbody>();
playerRigidBody.freezeRotation = true;
jumpEnabled = true;
}
void Update () {
//Timer
Gametimer.text = "Time: " + Time.realtimeSinceStartup.ToString();
//Player rotation, movement and jump.
playerTransform.Rotate(0, Input.GetAxis("Horizontal"), 0);
Vector3 forward = new Vector3(0f, 0.0f, Input.GetAxis("Forward"));
transform.Translate(forward * characterFSpeed * Time.deltaTime, Space.Self);
Vector3 backward = new Vector3(0f, 0.0f, Input.GetAxis("BackUp"));
transform.Translate(backward * characterBSpeed * Time.deltaTime, Space.Self);
if (Input.GetButton("Jump") && touchesGround && jumpEnabled)
{
touchesGround = false;
Jump.Play();
inTheAir = true;
StartCoroutine(co());
playerRigidBody.AddForce(0f, 150f, 0f);
}
}
IEnumerator co()
{
if (inTheAir && touchesGround)
{
Debug.Log("Detected");
jumpEnabled = false;
yield return new WaitForSeconds(5f);
jumpEnabled = true;
inTheAir = false;
}
}
void OnCollisionStay()
{
touchesGround = true;
Debug.Log("Touching the ground.");
}
}
You're using GetButton, when you should be using GetButtonDown, to prevent it playing twice, also in your IEnumerator co() change the 5f in your WaitForSeconds to 0.25f, as 1f is a second in unity.
Oh yes that fixed the sound issue, however when I lowered the seconds in the coroutine now he's given a double jump somehow. Even though he is only suppose to be able to jump if he is touching the ground.(touchesGround == true) It actually doesn't even print "Detected" to console, so it seems the coroutine isn't even started, very odd.
But it might be hard to spot the issue without having the files on hand so thanks for resolving my jump issue. I'll see if I can't mess around with this a bit further and figure out how to do it correctly.
$$anonymous$$y brain struggles with your logic of being in the air and touching the ground at the same time.
Answer by grimmdeveloper · Mar 09, 2018 at 10:40 AM
Add another condition to the if getbuttondown("jump") add a condition to check if intheair == false.
I solved the issue by adding a OnCollisionExit() a whole lot cleaner and now he jumps around without jumping twice. Also I find that the jumping somehow takes a bit longer to respond upon hitting the ground again. So there is no need for a delay after hitting the ground after having just jumped. Sound and jump issues solved, I got another issue with my self made axises not detecting the xbox joystick but I'll make another question for that unless there's an easy fix that you know of, cheers.
void Update () {
//Timer
Gametimer.text = "Time: " + Time.time.ToString();
//Player rotation, movement and jump.
playerTransform.Rotate(0, Input.GetAxis("Horizontal"), 0);
Vector3 forward = new Vector3(0f, 0.0f, Input.GetAxis("Forward"));
transform.Translate(forward * characterFSpeed * Time.deltaTime, Space.Self);
Vector3 backward = new Vector3(0f, 0.0f, Input.GetAxis("BackUp"));
transform.Translate(backward * characterBSpeed * Time.deltaTime, Space.Self);
if (Input.GetButtonDown("Jump") && touchesGround)
{
Debug.Log("Jumping");
Jump.Play();
playerRigidBody.AddForce(0f, 325f, 0f);
}
else
{
}
}
void OnCollisionStay()
{
touchesGround = true;
Debug.Log("Touching the ground.");
}
void OnCollisionExit()
{
touchesGround = false;
Debug.Log("Not touching the ground");
}
}
Input.GetAxis("Vertical"); ins$$anonymous$$d of Forward and BackUp. Vertical axis is used for up and down on the left joystick and horizontal is used for left and right on the same joystick.
Yes I know but I want different speeds depending on if he is moving forward or backing up. I managed to do that by creating two axises, it's working with the keyboard but it's not detected by the x box controller. :p
Your answer
Follow this Question
Related Questions
vibrating jumps and collisions 0 Answers
Touch control swipe up to jump 1 Answer
My game seems to be running too fast for certain tasks 0 Answers
problem with jump script 2 Answers
Help please! Why can't I reduce the speed before jumping? 1 Answer