- Home /
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
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
Your answer

Follow this Question
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