Polski Arrow
New member
- Joined
- Dec 1, 2019
- Messages
- 3
- Programming Experience
- Beginner
Hi, I was writing code for GUI in my Unity game and I started by making just layer for this. I wanted to test script showing and hiding cursor after clicking Tab and cursor was showing and hiding many times per second.
This is code fragment responsible for this function:
Here is recorded problem:
(BTW: Do you know how to turn off rotating after opening inventory or any other condition?)
And this is entire game code (if needed):
This is code fragment responsible for this function:
C#:
if (openinventory)
{
GUI.DrawTexture(new Rect(Screen.width - Screen.width / 1.2f, Screen.height - Screen.height / 1.2f, Screen.width * 0.6666f, Screen.height * 0.6666f), inventoryLabel);
Cursor.visible = true;
Cursor.lockState = wantedMode;
}
Here is recorded problem:
Cursor glitch.mp4
drive.google.com
And this is entire game code (if needed):
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStats : MonoBehaviour
{
private float maxHealth = 100;
private float currentHealth = 100;
private float maxArmour = 100;
private float currentArmour = 100;
private float maxStamina = 500;
private float currentStamina = 500;
private float maxHunger = 100;
private float currentHunger = 100;
private float HPbarWidth;
private float ARMbarWidth;
private float STbarWidth;
private float HGbarWidth;
private float barHeight;
private float barShadowWidth;
private float barShadowHeight;
public Texture2D HPtexture;
public Texture2D ARMtexture;
public Texture2D STtexture;
public Texture2D HGtexture;
public Texture2D shadowTexture;
public float walkSpeed = 10f;
public float runSpeed = 20f;
private float canHeal;
private float canRegenerate;
private bool hurt = false;
private bool hrt = false;
private bool blood = false;
private float sec;
private float opacity;
private float opacity2;
private float fullopacity = 100;
public Texture2D bloodTexture;
public Texture2D redScreen;
public Texture2D iconTexture;
public Texture2D iconFloor;
private bool leftIcon1 = false;
private bool leftIcon2 = true;
private bool leftIcon3 = false;
private bool grt2 = false;
private bool grt3 = false;
private bool glt2 = false;
private bool glt1 = false;
private bool rightIcon1 = false;
private bool rightIcon2 = true;
private bool rightIcon3 = false;
private bool gt1 = false;
private bool gt2 = false;
private bool gt3 = false;
private bool openinventory = false;
public Texture2D inventoryLabel;
CursorLockMode wantedMode;
private Vector3 lastPosition;
private CharacterController chCont;
private UnityStandardAssets.Characters.FirstPerson.FirstPersonController fpsC;
void Awake()
{
HPbarWidth = Screen.width - 20;
ARMbarWidth = Screen.width / 3 - 15;
STbarWidth = Screen.width / 3 - 15;
HGbarWidth = Screen.width / 3 - 15;
barHeight = HPbarWidth / 50;
barShadowWidth = Screen.width;
barShadowHeight = barHeight + 10;
lastPosition = transform.position;
chCont = GetComponent<CharacterController>();
fpsC = gameObject.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>();
}
private void OnGUI()
{
GUI.DrawTexture(new Rect(0, 0, barShadowWidth, barShadowHeight), shadowTexture);
GUI.DrawTexture(new Rect(0, Screen.height - barShadowHeight, barShadowWidth, barShadowHeight), shadowTexture);
GUI.DrawTexture(new Rect(10, 5, currentHealth * HPbarWidth / maxHealth, barHeight), HPtexture);
GUI.DrawTexture(new Rect(10, Screen.height - barHeight - 5, currentArmour * ARMbarWidth / maxArmour, barHeight), ARMtexture);
GUI.DrawTexture(new Rect(Screen.width / 3 + 5, Screen.height - barHeight - 5, currentStamina * STbarWidth / maxStamina, barHeight), STtexture);
GUI.DrawTexture(new Rect(Screen.width / 3 * 2 + 5, Screen.height - barHeight - 5, currentHunger * HGbarWidth / maxHunger, barHeight), HGtexture);
if (hurt)
{
opacity = 1;
blood = true;
hrt = true;
if (hrt)
{
hurt = false;
hrt = false;
}
}
if (blood && currentArmour <= 0)
{
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, opacity);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), bloodTexture, ScaleMode.ScaleToFit);
StartCoroutine("waitAndChangeOpacity");
}
if (opacity <= 0)
{
blood = false;
}
if (currentHealth <= 0)
{
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), bloodTexture, ScaleMode.ScaleToFit);
blood = false;
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, opacity2);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), redScreen);
StartCoroutine("halfOpacity");
}
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, fullopacity);
GUI.DrawTexture(new Rect(100, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
GUI.DrawTexture(new Rect(150, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
GUI.DrawTexture(new Rect(200, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
GUI.DrawTexture(new Rect(Screen.width - 140, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
GUI.DrawTexture(new Rect(Screen.width - 190, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
GUI.DrawTexture(new Rect(Screen.width - 240, Screen.height - barShadowHeight - 45, 40, 40), iconTexture);
if (grt2)
{
sec = 0.01f;
sec -= Time.deltaTime;
if (sec <= 0)
{
leftIcon2 = true;
leftIcon1 = false;
grt2 = false;
}
}
if (grt3)
{
sec = 0.01f;
sec -= Time.deltaTime;
if (sec <= 0)
{
leftIcon3 = true;
leftIcon2 = false;
grt3 = false;
}
}
if (glt2)
{
sec = 0.01f;
sec -= Time.deltaTime;
if (sec <= 0)
{
leftIcon2 = true;
leftIcon3 = false;
glt2 = false;
}
}
if (glt1)
{
sec = 0.01f;
sec -= Time.deltaTime;
if (sec <= 0)
{
leftIcon1 = true;
leftIcon2 = false;
glt1 = false;
}
}
if (leftIcon1)
{
GUI.DrawTexture(new Rect(100, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
}
if (leftIcon2)
{
GUI.DrawTexture(new Rect(150, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
}
if (leftIcon3)
{
GUI.DrawTexture(new Rect(200, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
}
if (gt1)
{
sec = 0.01f;
sec -= Time.deltaTime;
if (sec <= 0)
{
rightIcon1 = true;
rightIcon3 = false;
gt1 = false;
}
}
if (gt2)
{
sec = 0.01f;
sec -= Time.deltaTime;
if (sec <= 0)
{
rightIcon2 = true;
rightIcon1 = false;
gt2 = false;
}
}
if (gt3)
{
sec = 0.01f;
sec -= Time.deltaTime;
if (sec <= 0)
{
rightIcon3 = true;
rightIcon2 = false;
gt3 = false;
}
}
if (rightIcon1)
{
GUI.DrawTexture(new Rect(Screen.width - 240, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
}
if (rightIcon2)
{
GUI.DrawTexture(new Rect(Screen.width - 190, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
}
if (rightIcon3)
{
GUI.DrawTexture(new Rect(Screen.width - 140, Screen.height - barShadowHeight - 10, 40, 5), iconFloor);
}
if (openinventory)
{
GUI.DrawTexture(new Rect(Screen.width - Screen.width / 1.2f, Screen.height - Screen.height / 1.2f, Screen.width * 0.6666f, Screen.height * 0.6666f), inventoryLabel);
Cursor.visible = true;
Cursor.lockState = wantedMode;
}
}
private void FixedUpdate()
{
float speed = walkSpeed;
if (chCont.isGrounded && Input.GetKey(KeyCode.LeftShift) && lastPosition != transform.position && currentStamina > 0)
{
lastPosition = transform.position;
speed = runSpeed;
fpsC.CanRun = true;
currentStamina -= 1;
currentStamina = Mathf.Clamp(currentStamina, 0, maxStamina);
canHeal = 5;
}
else
{
fpsC.CanRun = false;
}
if (canHeal > 0)
{
canHeal -= Time.deltaTime;
if (canHeal <= 0)
{
canHeal = 0;
}
}
if (canHeal <= 0)
{
Regenerate(ref currentStamina, maxStamina);
}
if (canRegenerate > 0)
{
canRegenerate -= Time.deltaTime;
if (canRegenerate <= 0)
{
canRegenerate = 0;
}
}
if (canRegenerate <= 0)
{
Regenerate(ref currentHealth, maxHealth);
}
if (currentHealth <= 0)
{
canRegenerate = 1;
fpsC.CanWalk = false;
fpsC.CanJump = false;
}
if (currentStamina > 0)
{
fpsC.CanJump = true;
}
else if (currentStamina <= 0)
{
fpsC.CanJump = false;
}
if (currentHealth > 0)
{
fpsC.CanJump = true;
}
else if (currentHealth <= 0)
{
fpsC.CanJump = false;
}
}
void Regenerate(ref float currentStat, float maxStat)
{
currentStat += 1;
currentStat = Mathf.Clamp(currentStat, 0, maxStat);
}
void TakeHit(float damage)
{
if (currentArmour > 0)
{
currentArmour -= damage;
if (currentArmour < 1)
{
currentHealth += currentArmour;
currentArmour = 0;
canRegenerate = 5;
}
}
else if (currentArmour < 1)
{
currentHealth -= damage;
canRegenerate = 5;
if (currentHealth < 1)
{
currentHealth = 0;
}
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
TakeHit(30);
hurt = true;
}
if (Input.GetKeyDown(KeyCode.E) && leftIcon1)
{
grt2 = true;
}
if (Input.GetKeyDown(KeyCode.E) && leftIcon2)
{
grt3 = true;
}
if (Input.GetKeyDown(KeyCode.Q) && leftIcon3)
{
glt2 = true;
}
if (Input.GetKeyDown(KeyCode.Q) && leftIcon2)
{
glt1 = true;
}
if (Input.GetKeyDown(KeyCode.R) && rightIcon1)
{
gt2 = true;
}
if (Input.GetKeyDown(KeyCode.R) && rightIcon2)
{
gt3 = true;
}
if (Input.GetKeyDown(KeyCode.R) && rightIcon3)
{
gt1 = true;
}
if (Input.GetKeyDown(KeyCode.Space) && currentStamina > 0 && currentHealth > 0)
{
currentStamina -= 50;
currentStamina = Mathf.Clamp(currentStamina, 0, maxStamina);
canHeal = 5;
}
if (Input.GetKeyDown(KeyCode.Tab) && !openinventory)
{
openinventory = true;
}
else if (Input.GetKeyDown(KeyCode.Tab) && openinventory)
{
openinventory = false;
}
}
IEnumerator waitAndChangeOpacity()
{
yield return new WaitForEndOfFrame();
opacity -= 0.005f;
}
IEnumerator halfOpacity()
{
yield return new WaitForEndOfFrame();
opacity2 = 0.5f;
}
}