Question by
BalboaTheGreat · May 29, 2019 at 05:38 PM ·
variablesinconsistent
Why is this variable acting one way, but if I change it, and change it back exactly how it was, thus making no change, it starts acting another way?
This happens with other variables too sometimes, I have a variable named Jump Magnitude set to 26, if I test ingame, I cannot reach a platform by jumping, but if I change the variable to 27, then back to 26, or any arbitrary update to the value, I can suddenly reach the platform. Here is the code, but its quite messy as it handles more then just the jumping.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour {
public bool InputEnabled;
public int FacingDirection = 1;
public bool IsMoving = false;
public bool IsFalling = false;
public bool CanWalk = true;
public bool CanJump = true;
public bool IsGrounded = true;
public bool IsAgainstCeiling = false;
public bool IsAgainstWallLeft = false;
public bool IsAgainstWallRight = false;
public bool IsJumping;
public bool IsDoubleJumping;
public bool CanDoubleJump = false;
private bool WalkingBlocked = false;
public int MoveDirection;
public int MoveSpeed;
public float JumpFrames = 0.0f;
// Forced movement translation, mainly used for auto-walking during scene changes
public int TranslateX = 0;
private KeyCode JumpButton;
private KeyCode LeftButton;
private KeyCode RightButton;
public float JumpMagnitude;
public float JumpForce;
public bool DoubleJumpingEnabled = false;
private GameObject PlayerEntity;
private Rigidbody2D Body;
private BoxCollider2D GroundDetector;
private BoxCollider2D CeilingDetector;
private BoxCollider2D LeftWallDetector;
private BoxCollider2D RightWallDetector;
public LayerMask TerrainLayer;
private float TimeElapsedSinceInit = 0.0f;
public float SoundInitDelay = 1.0f;
public bool SoundsEnabled = false;
public AudioSource LandingSound;
// Start is called before the first frame update
void Start()
{
PlayerEntity = gameObject;
Body = PlayerEntity.GetComponent<Rigidbody2D>();
GroundDetector = gameObject.transform.Find("FloorDetector").gameObject.GetComponent<BoxCollider2D>();
CeilingDetector = gameObject.transform.Find("CeilingDetector").gameObject.GetComponent<BoxCollider2D>();
LeftWallDetector = gameObject.transform.Find("LeftWallDetector").gameObject.GetComponent<BoxCollider2D>();
RightWallDetector = gameObject.transform.Find("RightWallDetector").gameObject.GetComponent<BoxCollider2D>();
WalkingBlocked = false;
IsMoving = false;
IsFalling = false;
IsGrounded = true;
IsAgainstCeiling = false;
IsAgainstWallLeft = false;
IsAgainstWallRight = false;
MoveDirection = 0;
//controls
JumpButton = KeyCode.Space;
LeftButton = KeyCode.A;
RightButton = KeyCode.D;
}
// Update is called once per frame
void Update()
{
Debug.Log(JumpMagnitude.ToString());
if (TimeElapsedSinceInit < SoundInitDelay)
{
TimeElapsedSinceInit = TimeElapsedSinceInit + Time.deltaTime;
}
else
{
SoundsEnabled = true;
}
if (GroundDetector.IsTouchingLayers(TerrainLayer))
{
if (IsGrounded == false & SoundsEnabled == true)
{
float RandPitch = (Random.Range(75.0f, 100.0f) / 100);
LandingSound.pitch = RandPitch;
LandingSound.Play();
}
IsGrounded = true;
IsFalling = false;
if (JumpFrames < (JumpMagnitude-10.0f))
{
JumpFrames = 0.0f;
}
}
else
{
IsGrounded = false;
IsFalling = true;
}
if (CeilingDetector.IsTouchingLayers(TerrainLayer))
{
IsAgainstCeiling = true;
JumpFrames = 0.0f;
}
else
{
IsAgainstCeiling = false;
}
if (LeftWallDetector.IsTouchingLayers(TerrainLayer))
{
IsAgainstWallLeft = true;
}
else
{
IsAgainstWallLeft = false;
}
if (RightWallDetector.IsTouchingLayers(TerrainLayer))
{
IsAgainstWallRight = true;
}
else
{
IsAgainstWallRight = false;
}
//
//INPUT
//
if (InputEnabled == true)
{
if (UnityEngine.Input.GetKey(JumpButton) & IsJumping == false & IsGrounded == true & CanJump == true)
{
IsJumping = true;
JumpFrames = JumpMagnitude;
}
MoveDirection = 0;
if (UnityEngine.Input.GetKey(LeftButton))
{
MoveDirection = MoveDirection - 1;
FacingDirection = -1;
}
if (UnityEngine.Input.GetKey(RightButton))
{
MoveDirection = MoveDirection + 1;
FacingDirection = 1;
}
}
if (UnityEngine.Input.GetKey(JumpButton) == false | InputEnabled == false)
{
JumpFrames = 0.0f;
if (IsGrounded == true)
{
IsJumping = false;
IsDoubleJumping = false;
CanDoubleJump = false;
}
else if (DoubleJumpingEnabled == true & IsDoubleJumping == false)
{
CanDoubleJump = true;
}
}
if (UnityEngine.Input.GetKey(JumpButton) & IsGrounded == false & CanDoubleJump == true & InputEnabled == true)
{
IsDoubleJumping = true;
CanDoubleJump = false;
JumpFrames = JumpMagnitude;
}
//
//END OF INPUT
//
if (JumpFrames > 0.0f & IsAgainstCeiling == false)
{
//Body.AddForce(new Vector2(0, JumpForce * JumpMagnitude));
Body.velocity = (Vector3.right * Body.velocity.x) + (Vector3.up * JumpForce);
//Body.velocity.Set(Body.velocity.x, ((JumpForce * PlayerEntity.GetComponent<BoxCollider2D>().size.y) / 10) * (JumpFrames / 10));
//PlayerEntity.transform.Translate(Vector3.up * (((JumpForce*PlayerEntity.GetComponent<BoxCollider2D>().size.y)/10) * (JumpFrames/10)));
JumpFrames = JumpFrames - 1.0f;
}
if (JumpFrames < 1.0f)
{
JumpFrames = 0.0f;
}
if ((MoveDirection > 0 & IsAgainstWallRight) | (MoveDirection < 0 & IsAgainstWallLeft))
{
WalkingBlocked = true;
}
else
{
WalkingBlocked = false;
}
if (CanWalk == true & WalkingBlocked == false)
{
//PlayerEntity.transform.Translate(Vector3.right * MoveSpeed * Time.deltaTime * MoveDirection);
if (TranslateX != 0)
{
MoveDirection = TranslateX / System.Math.Abs(TranslateX);
}
Body.velocity = (Vector3.up * Body.velocity.y) + (Vector3.right * MoveSpeed * MoveDirection);
if (MoveDirection != 0)
{
IsMoving = true;
}
else
{
IsMoving = false;
}
}
}
}
Here is a visual: https://youtu.be/8eXAlAeU6iY
Comment
Your answer
Follow this Question
Related Questions
Getting an object's y coordinate 2 Answers
How to change text based on the value of a scrollbar? 1 Answer
Unreferenced label? 0 Answers
Pass a variable into a class on creation 1 Answer
Pickup Animation for Closest Object 1 Answer