Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by wermon · Feb 01, 2021 at 02:48 PM · movementboolean

Move Problems Using a bool

HOW ARE PEOPLE, I AM ENDING A GAME WITH LEAP MOTION, COMPARING THE POSITION I MAKE A BOOLEAN CHANGE THE CHARACTER'S DIRECTION, BUT IS ENTERING THE CONDITION, IT DOES NOT WORK AS IT SHOULD TAKE ONLY ONE OF THE SIDES. WHAT CAN BE THE ERROR?

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour 
 {
     public Sprite sprite1;
     public Sprite sprite2;
     public Sprite sprite3;
     const float kGravity = 9.8f;
     public bool DirIzquierda;
     public bool DirDerecha;
     public Camera m_gameCamera;
     public LevelManager m_levelManager;
     public float m_maxYVel = 10.0f;
     public float m_maxXVel = 5.0f;
     public float m_jumpAmount = 8.0f;
     public float m_strafeSensitivity = 50.0f;
     public float m_strafeResistance = 10.0f;
     public float m_gravityIncDampener = 0.5f; // NOTE: This is just a tweakable magic number
     public float m_jumpIncDampener = 0.1f; // NOTE: This is just a tweakable magic number
 
     private Platform m_onPlatform = null;
     private float m_levelWidth = 0.0f;
     private float m_levelHeight = 0.0f;
     private float m_xPos = 0.0f;
     private float m_yPos = 0.0f;
     private float m_xVel = 0.0f;
     private float m_yVel = 0.0f;
     private float m_currSpeed = 1.0f;
 
     public void SetSpeed(float speed)
     {
         m_currSpeed = speed;
     }
 
     void Start ()
     {
         m_xPos = transform.position.x;
         m_yPos = transform.position.y;
 
         Vector3 leftCornerPos = m_gameCamera.ViewportToWorldPoint( new Vector3( 1.0f, 1.0f, m_gameCamera.nearClipPlane) );
         m_levelWidth = leftCornerPos.x * 2.0f;
         m_levelHeight = leftCornerPos.y * 2.0f; 
     }
 
     void FixedUpdate () 
     {
 //        Debug.Log("sensitivity"+m_xVel);
         
 //        Debug.Log(m_onPlatform);
         UpdateJump ();
         UpdateStrafe (); 
         UpdateState ();
         UpdatePos ();
     }
 
     void UpdateJump()
     {
         m_yVel -= (kGravity * (1.0f + (m_currSpeed*m_gravityIncDampener))) * Time.fixedDeltaTime;
         if (IsOnPlatform ()) 
         {
             m_yVel = -m_onPlatform.m_speed;
             m_yVel += m_jumpAmount * (1.0f + (m_currSpeed*m_jumpIncDampener) );
         }
     }
 
     void UpdateStrafe()
     {
         bool touchLHS = false;
         bool touchRHS = false;
         for (int i = 0; i < Input.touchCount; ++i) 
         {
             float touchX = Input.GetTouch (i).position.x;
             if (touchX < (Screen.width/2.0f) )
             {
                 touchLHS = true;
             }
             else
             {
                 touchRHS = true;
             }
         }
 
         if (DirDerecha==true || touchLHS)                                                               /////////HERE I CAHNGE FROM OTHER CODE DE BOOL AND THEN CHECK TRUE BUT DONT WORK WELL
         {
                this.gameObject.GetComponent<SpriteRenderer>().flipX = true;
 
             m_xVel -= m_strafeSensitivity * Time.fixedDeltaTime;
         }
         
         if (DirIzquierda == true || touchRHS) 
         {
                 this.gameObject.GetComponent<SpriteRenderer>().flipX = false;
 
             m_xVel += m_strafeSensitivity * Time.fixedDeltaTime;
         }
 
         if (m_xVel > 0) 
         {
             m_xVel = Mathf.Clamp (m_xVel - (m_strafeResistance * Time.fixedDeltaTime), 0, m_maxXVel);
         } 
         else if (m_xVel < 0)
         {
             m_xVel = Mathf.Clamp (m_xVel + (m_strafeResistance * Time.fixedDeltaTime), -m_maxXVel, 0);
         }
     }
 
     void UpdateState()
     {
         m_xPos += m_xVel * Time.fixedDeltaTime;
         m_yPos += m_yVel * Time.fixedDeltaTime;
 
         const float kWallWidth = 0.6f;
         m_xPos = Mathf.Clamp (m_xPos, (-m_levelWidth/2.0f) + kWallWidth, (m_levelWidth/2.0f) - kWallWidth);
         m_yPos = Mathf.Clamp (m_yPos, -m_levelHeight/2.0f, m_levelHeight/2.0f);
 
         if (m_yPos == m_levelHeight / 2.0f) 
         {
             m_yVel = 0;
         }
 
         if (m_yPos == -m_levelHeight / 2.0f) 
         {
             m_levelManager.GameOver();
         }
     }
 
     void UpdatePos()
     {
         transform.position = new Vector2 (m_xPos, m_yPos);
     }
 
     bool IsOnPlatform()
     {
         return m_onPlatform != null;
         
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Platform") 
         {
             StartCoroutine(ExampleCoroutine());
             Debug.Log("estoy en la plataforma");
             float platformHeight = other.bounds.max.y;
             if (m_yVel < 0.0f && m_yPos > platformHeight) 
             {
                 SpriteRenderer sprite = GetComponent<SpriteRenderer> ();
                 Platform platform = other.GetComponent<Platform> ();
                 m_onPlatform = platform;
                 m_yPos = platformHeight + (sprite.bounds.extents.y - 0.1f);
             }
         }
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         this. gameObject.GetComponent<SpriteRenderer>().sprite = sprite2;
         m_onPlatform = null;
     }
         IEnumerator ExampleCoroutine()
 {      
       yield return new WaitForSeconds(0.3f);
 this. gameObject.GetComponent<SpriteRenderer>().sprite = sprite1;
 yield return new WaitForSeconds(0.3f);
 this. gameObject.GetComponent<SpriteRenderer>().sprite = sprite3;
 }
 }
 

Comment
Add comment · Show 1
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
avatar image wermon · Feb 01, 2021 at 02:54 PM 0
Share

A boolean that changes according to the position of another object, replaces the getkey condition with this one, the verification process works, but the movement is not as expected

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

166 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

Related Questions

Slow down character while reloading. 2 Answers

Show an ad once every 2 times 2 Answers

How to use 'check if player is on ground' bool in script 2 Answers

Stop Player movement when bool changes 2D 3 Answers

How do I stop character movement when attacking? 0 Answers


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