Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

176 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges