- Home /
jumping issues
in unity i have a character, who will jump( without the if tag), but as many times as he wants. if i put that if tag, i woundnt jump at all. Help with my code C#, or please let me know the soliution to my problem (bold) MY CODE ----
using UnityEngine;
public class movement : MonoBehaviour {
private Rigidbody rb;
public float speed;
public float jumpvelocity;
float jumptime = 0f;
public KeyCode up; // The button is the letter q
float adjsVelsocityY;
public void Start()
{
rb = GetComponent<Rigidbody>();
}
public void FixedUpdate()
{
adjsVelsocityY = 0f;
if (Input.GetKeyDown(up))
{
if (jumptime >= 1)
{
adjsVelsocityY = jumpvelocity;
jumptime = jumptime + 1;
}
}
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y + adjsVelsocityY, rb.velocity.z);
adjsVelsocityY = 0f;
}
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("ground")) //i did set the tag
{
jumptime = 0f;
}
}
The problem is that you only increase jumpTime when you are jumping, but to jump jumpTime already has to be at least 1.
I'm not sure what you want to achieve with the jumpTime variable, but I think you should move the jumpTime = jumpTime +1; line just before the if (jumpTime >= 1) line.
Also, I think you want to use a non-trigger collider for your ground, because trigger colliders do not stop movement with physics (they are just detectors of collision, for triggering, hence the name). So I would use OnCollisionEnter (Collision collision), and uncheck the trigger on the ground collider.
Finally, some coding suggestions: you can have a local adjsVelocityY variable in FixedUpdate(), you don't need to have it as a class variable. Then its value is not kept between calls to FixedUpdate(), so you don't need to reset it to 0. Also, you can write someIntVariable = someIntVariable + 1; simpler like this someIntVariable++;
shouldn't it read jumptime += 1;?maybe that is your the if statement doesn't work because you aren't adding 1 to jumptime
adjsVelsocityY = jumpvelocity;
jumptime += 1;
jumpTime += 1; is literally the same as jumpTime = jumpTime + 1; (or as I suggested, jumpTime++;, or also, ++jumpTime;, and I'm sure there other ways as well).
ok, thanks. Good to know. I learn more than I inform here, that's for sure. until recently I had always been writing if(other.tag==player) rather than the more efficient if(other.CompareTag ("Player"))
Your answer