- Home /
Cant jump while running left or right c#?
Hi 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.
Answer by vinfang · Dec 30, 2013 at 08:00 AM
I didn't see anything obvious to help give you a quick answer, but since you're using MonoDevelop, I suggest you use the debugger to help narrow down what section of code you need to look at on what's wrong. Just put a breakpoint on line 17 in the PlayerJump.cs, start the debugger by attaching to the Unity process, run the game, and step over each line and mouse over or look at the local variables to see what their values are after each line of code is executed to see if the code is behaving as you expected.
If you're unfamiliar with using the debugger, just look for some simple quick tutorials and they should give you the basic concept.
Answer by sattri99 · Dec 30, 2013 at 09:21 AM
I think the problem is in your first script player logic. When you want to move your player you create a translatory motion using transfor. Translate. But when you applied jump you use AddForce. I think these two motions contradict each other and may not work together properly. I will suggest you to use Addforce instead of transform.Translate for moving left or right. I've also created and uploaded a project on my blog in which there is perfect 2d character motion using rigidbody and addforce. I think you should check it out on my blog www.physicist3d.blogspot.com
Thanks so much, Ive been looking for tutorials on Unitys new 2D System and cant seem to find any that make a complete a 2d platformer. Thankyou
Answer by sath · Dec 30, 2013 at 09:21 AM
in PlayerLogic at line 45 :
if(Jump == true && grounded == true)
{
rigidbody2D.AddForce(Vector2.up * jumpForce);
Jump = false;
}
you call player to Jump and immediately you tell him jump=false ...
at line 64 :
if(Input.GetKeyDown (KeyCode.Space) && grounded)
{
rigidbody2D.AddForce(Vector2.up * jumpForce);
}
is working 'couse you dont tell him jump=false
Yes thanks for the reply but im using touch controls and have to set a jump variable to true then stop the jump by say false or the player will keep flying into the sky..
if you have jump animation at the time you tap to jump the animation is playing then you can say :
if(animation["your_jump_name"].normalizedTime>0.2f){
jump=false;
}
that's one solution but of course there are many others, find the one that fits in your situation..
If you want that the jump became false after the player has jumped off the ground you can easily create this effect by writing the "jump = false" in function late update. I've also tested this and implemented in my 2D Platformer game
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Jump and move (CharacterController.velocity) C# 0 Answers
My Player can't jump - coding 1 Answer
Click to Move With Navmesh 0 Answers
[C#] Jump on slopes 1 Answer