Hi Im new to the unity community but not Unity and programming. I am making a 2D Platformer in Unity 4.3 (new 2D Tools) and everything seemed to be going just fine until I added Jumping… My character Jumps fine but my problem is that he wont jump while Running left or right. My character does this fine on a windows computer but when I compile a .APK and run it on my android tablet with touch controls (GuiTextures) he simply will not Jump while running left or Right.
Here are my scripts…
PlayerLogic.cs
using UnityEngine;
using System.Collections;
public class PlayerLogic : MonoBehaviour {
public Transform GroundStart, GroundEnd;
public bool grounded = false;
public float jumpForce = 300f;
public float MoveSpeed = 3f;
public int JumpCount = 0;
//Movement booleans
public static bool MoveRight = false;
public static bool MoveLeft = false;
public static bool Jump = false;
// Use this for initialization
void Update()
{
//Run these to functions
Raycasting();
Movement();
}
void Raycasting()
{
//Draw a line from the start position, to the end position.
Debug.DrawLine(this.transform.position, GroundEnd.position, Color.green);
//If Raycast detects the Ground layer from the end point of the line, then IsGrounded becomes true
grounded = Physics2D.Linecast(this.transform.position, GroundEnd.position, 1 << LayerMask.NameToLayer("Ground"));
}
void Movement()
{
//Touch Controls
if(MoveRight == true)
{
transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if(MoveLeft == true)
{
transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 180);
}
if(Jump == true && grounded == true)
{
rigidbody2D.AddForce(Vector2.up * jumpForce);
Jump = false;
}
//Key Controls
/*if(Input.GetKey (KeyCode.D))
{
transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if(Input.GetKey (KeyCode.A))
{
transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 180);
}
if(Input.GetKeyDown (KeyCode.Space) && grounded)
{
rigidbody2D.AddForce(Vector2.up * jumpForce);
}*/
}
}
PlayerLeft.cs
using UnityEngine;
using System.Collections;
public class PlayerLeft : MonoBehaviour {
// Update is called once per frame
void Update ()
{
//Is there a touch?
if(Input.touches.Length <= 0)
{
//If no touches...
}
else //If screen touched..
{
//Loop through these
for(int i = 0; i < Input.touchCount; i++)
{
//executes this code for current touch
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
//If current touch hits the guitexture...
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
PlayerLogic.MoveLeft = true;
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
PlayerLogic.MoveLeft = false;
}
}
}
}
}
}
PlayerRight.cs
using UnityEngine;
using System.Collections;
public class PlayerRight : MonoBehaviour {
// Update is called once per frame
void Update ()
{
//Is there a touch?
if(Input.touches.Length <= 0)
{
//If no touches...
}
else //If screen touched..
{
//Loop through these
for(int i = 0; i < Input.touchCount; i++)
{
//executes this code for current touch
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
//If current touch hits the guitexture...
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
PlayerLogic.MoveRight = true;
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
PlayerLogic.MoveRight = false;
}
}
}
}
}
}
PlayerJump.cs
using UnityEngine;
using System.Collections;
public class PlayerJump : MonoBehaviour {
// Update is called once per frame
void Update ()
{
//Is there a touch?
if(Input.touches.Length <= 0)
{
//If no touches...
}
else //If screen touched..
{
//Loop through these
for(int i = 0; i < Input.touchCount; i++)
{
//executes this code for current touch
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
//If current touch hits the guitexture...
if(guiTexture.HitTest(Input.touches[0].position))
{
PlayerLogic.Jump = true;
}
}
}
}
}
}
I hope you can understand my question.
Thanks in advance.