- Home /
Bool is not turning false
Hi i'm trying to create a MMORPGG (Massive Multiplayer Online Role-Playing Gun Game) in unity and i can't fix my playerController(Character movement using C#) and playerScript(Used it for the UI)...
Here's the playerController Script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(PlayerScript))]
public class PlayerController : MonoBehaviour
{
public float crchSpeed = 1;
public float walkSpeed = 5;
public float runSpeed = 8;
public float jumpForce = 5;
public float gravity = 50;
public float rotation = 30;
public bool hasStamina = true;
public bool isWalking = false;
public bool isRunning = false;
public bool jumping = false;
CharacterController cc;
PlayerScript ps;
private Vector3 motion = Vector3.zero;
// Use this for initialization
void Start ()
{
cc = GetComponent<CharacterController>();
ps = GetComponent<PlayerScript>();
}
// Update is called once per frame
void Update ()
{
HandleMovement();
cc.SimpleMove(Physics.gravity);
motion.y -= gravity * Time.deltaTime;
cc.Move(motion * Time.deltaTime);
if(isRunning == true && Input.GetKeyUp(KeyCode.W))
{
isRunning = false;
}
}
void HandleMovement ()
{
if(cc.isGrounded)
{
if(Input.GetKey(KeyCode.W))
{
isWalking = true;
}
else
{
isWalking = false;
}
if(isRunning == false || ps.stamina == 0)
{
motion = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
motion = transform.TransformDirection(motion);
motion *= walkSpeed;
}
if(isWalking == true)
{
if(Input.GetButton("Sprint") && hasStamina == true)
{
motion = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
motion = transform.TransformDirection(motion);
motion *= runSpeed;
isRunning = true;
}
else
{
if(Input.GetKeyUp(KeyCode.W) || Input.GetButtonUp("Sprint") || hasStamina == false)
{
isRunning = false;
isWalking = false;
}
isRunning = false;
}
}
if(isRunning == true && Input.GetKeyUp(KeyCode.W) || hasStamina == false)
{
isRunning = false;
}
if(Input.GetButton("Crouch"))
{
motion = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
motion = transform.TransformDirection(motion);
motion *= crchSpeed;
}
if(Input.GetButton("Jump"))
{
motion.y = jumpForce;
jumping = true;
}
else
{
jumping = false;
}
}
}
}
And here's my playerScript:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerScript : MonoBehaviour
{
public float speed;
public RectTransform healthTransform;
private float cachedY;
private float minXValue;
private float maxXValue;
private int currentHealth;
private int CurrentHealth
{
get { return currentHealth;}
set {
currentHealth = value;
HandleHealth();
}
}
public int maxHealth;
//public Text healthText;
public Image visualHealth;
public float coolDown;
private bool onCD;
public RectTransform staminaTransform;
private float sCachedY;
private float minXValueS;
private float maxXValueS;
public int stamina;
private int currentStamina;
private int CurrentStamina
{
get { return currentStamina;}
set {
currentStamina = value;
HandleStamina();
}
}
public int maxStamina;
public Image visualStamina;
PlayerController pc;
// Use this for initialization
void Start ()
{
cachedY = healthTransform.position.y;
maxXValue = healthTransform.position.x;
minXValue = healthTransform.position.x - healthTransform.rect.width;
currentHealth = maxHealth;
onCD = false;
sCachedY = staminaTransform.position.y;
maxXValueS = staminaTransform.position.x;
minXValueS = staminaTransform.position.x - staminaTransform.rect.width;
currentStamina = maxStamina;
pc = GetComponent<PlayerController>();
stamina = CurrentStamina;
}
public void OnTriggerStay(Collider other)
{
if(other.name == "Heal")
{
//Debug.Log("Getting Healed");
if(!onCD && currentHealth < maxHealth)
{
StartCoroutine(CoolDownDmg());
CurrentHealth += 1;
}
}
if(other.name == "Damage")
{
//Debug.Log("Taking Damage");
if(!onCD && currentHealth > 0)
{
StartCoroutine(CoolDownDmg());
CurrentHealth -= 1;
}
}
}
public void HandleHealth()
{
//healthText.text = currentHealth + "/100";
float currentXValue = MapValues (currentHealth, 0, maxHealth, minXValue, maxXValue);
healthTransform.position = new Vector3(currentXValue, cachedY);
/*if(currentHealth > maxHealth/2)//Then i have more than 50%
{
visualHealth.color = new Color32((byte)MapValues(currentHealth, maxHealth / 2, maxHealth, 255, 0), 255, 0, 255);
}
else //less than 50%
{
visualHealth.color = new Color32(255, (byte)MapValues(currentHealth , 0, maxHealth / 2 , 0 , 255), 0, 255);
}*/
}
IEnumerator CoolDownDmg()
{
onCD = true;
yield return new WaitForSeconds(coolDown);
onCD = false;
}
private float MapValues(float x, float inMin, float inMax, float outMin, float outMax)
{
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
//THE CODES BELOW IS FOR THE STAMINA CONTROLLER
public void HandleStamina()
{
//healthText.text = currentHealth + "/100";
float currentXValue = MapValuesStamina (currentStamina, 0, maxStamina, minXValueS, maxXValueS);
staminaTransform.position = new Vector3(currentXValue, sCachedY);
}
public void Update()
{
Movement ();
}
void Movement ()
{
if(pc.isRunning == true)
{
if(!onCD && currentStamina > 0 && pc.hasStamina == true)
{
StartCoroutine(CoolDownDmg());
CurrentStamina -= 3;
}
}
if(pc.isRunning == false)
{
if(!onCD && currentStamina < maxStamina && pc.isWalking == true)
{
StartCoroutine(CoolDownDmg());
CurrentStamina += 1;
}
if(!onCD && currentStamina < maxStamina && pc.isWalking == false)
{
StartCoroutine(CoolDownDmg());
CurrentStamina += 2;
}
}
if(CurrentStamina == 0)
{
pc.hasStamina = false;
}
if(CurrentStamina >= 150)
{
pc.hasStamina = true;
}
}
private float MapValuesStamina(float x, float inMin, float inMax, float outMin, float outMax)
{
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
}
The program is hasStamina will always be true and if the player is walking the bool in playerController isWalking is gonna be true and when i press the sprint button, the isRunning is gonna be true and the stamina bar in playerScript is gonna move backward so that it will look like the stamina is getting lower and when the stamina bar reaches the corner or it turns into 0 the hasStamina should turn false... but in my program its not turning false.. the player just keeps running and if i let go of the sprint button or the W key the hasStamina will turn false and i can't run anymore... it should be like when the stamina reaches 0 the hasStamina should turn false and the playerController should return to walkspeed not runspeed..
please help... you can use my code for your games if you want
here's my 2c - not that anyone asked for it ;)
i think that the larger problem is that code needs to be simplified and made more consistent. here are some examples of why i believe this:
you've got some potentially conflicting variables names - isWalking
& isRunning
probably shouldn't be separate (use a player state ins$$anonymous$$d), but if you wish to maintain them as booleans, shouldn't you be setting both at the same time since the player can't be walking and running at the same time. you seem to do this in some places, but not everywhere.
from a readability perspective, you sometimes test a boolean with == true/false
but elsewhere you use something like if (!onCD...)
- again, making this consistent might help with spotting logic errors. it might not seem important, but it can help. if you're posting this code and asking for help, maintaining that consistency will make your code easier to read and make it more likely to get you the help that you want/need.
in Handle$$anonymous$$ovement()
, you've got this:
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W) || Input.GetButtonUp("Sprint") || hasSta$$anonymous$$a == false)
{
isRunning = false;
isWalking = false;
}
isRunning = false;
setting isRunning
to false
in the conditional check is not needed.
when explaining the problem, try to be concise and re-read your post ... "The program is hasSta$$anonymous$$a will always be true ..." shouldn't that be "The problem is..."? most of your explanation is unnecessary because we can deduce that from the code (which is why consistency is important).
and try to remove sections of code which have been commented out - it's just more for us to read before we get to the actual problem.
finally, "you can use my code for your games if you want" - with all respect, nobody will use code that doesn't work.
i'll stop now, you probably didn't read this far anyway...
Looks like the condition for switching hasSta$$anonymous$$a
is (CurrentSta$$anonymous$$a == 0)
. Given that you're subtracting 3 for running, and adding 1 or 2 back for regenerating, who's to say it isn't going negative and never hitting 0?
gjf, the playerScript is not literally $$anonymous$$e, i was following a tutorial on youtube on how to use Unity's new UI but then he started using IEnumerator and stuff.. sorry if the code is too long... the problem is in the playerController, something there is keeping the hasSta$$anonymous$$a bool true while the playerScript is trying to turn it to false... i really need help on that sta$$anonymous$$a thingy...
To echo @gjf 's sentiment, having stuff like int sta$$anonymous$$a
and then a separate bool hasSta$$anonymous$$a
on another script is just asking for trouble. hasSta$$anonymous$$a
shoud be a method that checks if sta$$anonymous$$a > 0
. Always try to have just 1 variable for 1 piece of information because then you can't have variables in conflicting states, which probably is the problem here too.
again, sorry if my code is too long and complicated.. i'll try to fix the code by my own for a while and post a comment if it will work or not
Answer by EpicGhani · Feb 06, 2015 at 07:55 AM
i fixed the program! i just changed some stuff in the playerScript and it started working like a charm! now i'm gonna try to make the program more simple
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
UnityEngine.Object is null but System.Object is NOT null! 3 Answers
Board Game Movement 0 Answers
Having problems with my swipe 0 Answers