- Home /
Problem with jump bool. Help?!
I've got what seems like a decent movement script after following a tutorial, but there is just one problem that is really annoying me and I cannot figure out! I've tried several different things over the past couple days, stuff that I was certain would fix it.. until it didn't. My jump bool does not act like it should, or at least how I believe it should. Jumping itself seems to be fine, just the bool is acting funky. For instance, as it is now, if I jump the bool does not get checked, if I jump again while in the air it DOES get checked, then automatically jumps again once the player has landed. I just want it to get checked when I hit jump and then uncheck itself after, while the player is in the air, basically. Anyone see the problem in the code? Should I even worry about it since it seems to be working correctly other than the bool function and just go ahead and make it to where he can't jump while grounded?... Or what? Lol. I've racked my brain and I'm stuck. I know it's something simple.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))] //This script requires a CharacterController
public class Unit : MonoBehaviour
{
protected CharacterController charController; //Reference to the CharacterController
protected Vector3 move = Vector3.zero;
public float walkSpeed = 1f;
public float runSpeed = 5f;
public float turnSpeed = 90f;
public float jumpSpeed = 5f;
protected bool jump;
protected bool running;
protected Vector3 gravity = Vector3.zero;
// Use this for initialization
public virtual void Start ()
{
charController = GetComponent<CharacterController>(); //Assigns CharacterController to it's reference (charController)
if (!charController) //If there is no CharacterController attached..
{
Debug.LogError ("Unit.Start() " + name + " has no CharacterController!"); //..log error..
enabled = false; // .. and disable the script.
}
}
// Update is called once per frame
public virtual void Update ()
{
if (running)
{
move *= runSpeed;
}
else
{
move *= walkSpeed;
}
if (!charController.isGrounded)
{
gravity += Physics.gravity * Time.deltaTime;
}
else
{
gravity = Vector3.zero;
if (jump)
{
gravity.y = jumpSpeed;
jump = false;
}
}
move += gravity;
charController.Move (move * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
public class UnitPlayer : Unit
{
// Use this for initialization
public override void Start ()
{
base.Start();
}
// Update is called once per frame
public override void Update ()
{
//rotation
transform.Rotate (0f, Input.GetAxis ("Turn") * turnSpeed * Time.deltaTime, 0f);
//movement
move = new Vector3(Input.GetAxis ("Strafe"), 0f, Input.GetAxis("Move"));
move.Normalize(); //fixes diagnol movement speed
move = transform.TransformDirection (move); //makes player move in look direction
if (Input.GetButtonDown ("Jump"))
{
jump = true;
}
running = Input.GetKey (KeyCode.LeftControl) || Input.GetKey (KeyCode.RightControl);
base.Update();
}
}
Your answer
Follow this Question
Related Questions
How can I do a foreach loop for an array of booleans? 1 Answer
Toggle bool with keypress -2 Answers
Boolean and input relation 0 Answers
I'm having trouble setting a bool. 2 Answers
GUI Tickbox (Boolean) 1 Answer