Cannot Jump while moving, using Touch Controls for Android
So, I am very new to Unity, and coding in general. I watched a few videos online on how to compile some sort of code for basic movement and jumping / double jumping abilities. Whenever I move my cube, using WASD and Spacebar for the jumping action, everything seems to work. However, I recently ported over to Android, and added in Touch controls, I used the standard asset single joy stick with the jump button as the touch control. I have my phone connected using the unity app, and moving left and right is perfect. Jumping is also perfect with the jump button. However, moving left and jumping is an issue, as well as moving right and jumping. It will not let me jump, but if I am moving left, the joystick will return to its original position for a split second, and i will stop moving, but will move on the next frame. Same thing with moving right, however it is not as noticeable. So my question is, how can I fix this bug? Honestly, it feels like Unity is detecting if I only had one mouse, or for laymans terms, one thumb, and using simultaneous inputs(thumbs) is confusing the system. Im not entirely sure what to do. I will post the player script that I am using, and if i need any other scripts for cross reference, let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour {
private float inputDirection; //This is the X value of our MoveVector
private float verticalVelocity; //This is the Y value of our MoveVector
private float speed = 5.0f;
private float gravity = 25.0f;
private float jumpForce = 10.0f;
private bool secondJumpAvail = false;
private Vector3 moveVector;
private Vector3 lastMotion;
private CharacterController controller;
// Use this for initialization, at the start of the game
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
IsControllerGrounded();
moveVector = Vector3.zero;
inputDirection = CrossPlatformInputManager.GetAxisRaw("Horizontal") * speed;
if (IsControllerGrounded()) {
verticalVelocity = 0;
if (CrossPlatformInputManager.GetButtonDown("Jump")) {
//Make the player jump!
verticalVelocity = jumpForce;
secondJumpAvail = true;
}
moveVector.x = inputDirection;
}
else {
if (CrossPlatformInputManager.GetButtonDown("Jump")) {
if(secondJumpAvail) {
verticalVelocity = jumpForce;
secondJumpAvail = false;
}
}
verticalVelocity -= gravity * Time.deltaTime;
moveVector.x = lastMotion.x;
}
moveVector.y = verticalVelocity;
controller.Move(moveVector * Time.deltaTime);
lastMotion = moveVector;
}
private bool IsControllerGrounded() {
Vector3 leftRayStart;
Vector3 rightRayStart;
leftRayStart = controller.bounds.center;
rightRayStart = controller.bounds.center;
leftRayStart.x -= controller.bounds.extents.x;
rightRayStart.x += controller.bounds.extents.x;
Debug.DrawRay(leftRayStart,Vector3.down,Color.red);
Debug.DrawRay(rightRayStart, Vector3.down, Color.green);
if(Physics.Raycast(leftRayStart, Vector3.down, (controller.height / 2) + 0.1f))
return true;
if (Physics.Raycast(rightRayStart, Vector3.down, (controller.height / 2) + 0.1f))
return true;
return false;
}
private void OnControllerColliderHit(ControllerColliderHit hit) {
Debug.Log(hit.gameObject.name);
if (controller.collisionFlags == CollisionFlags.Sides) {
if (CrossPlatformInputManager.GetButtonDown("Jump")) {
Debug.DrawRay(hit.point, hit.normal, Color.red, 2.0f);
moveVector = hit.normal * speed;
verticalVelocity = jumpForce;
secondJumpAvail = true;
}
}
}
} `
post using the codeBlock button that way it appears as code.
Answer by FuNnYm4n911 · Jul 11, 2018 at 05:39 AM
bump. can anyone help?
Thanks for posting your code block! And I have encountered this problem before. I will look into it and get back to you:)
Awesome! let me know if you find any solutions!
Your answer
Follow this Question
Related Questions
Android Controls 1 Answer
Read and Write Textfile on mobile (C# Unity ) 0 Answers
ScreenToWorldPoint on One Axis? 0 Answers
Move Character using Finger Gestures for Unity 0 Answers
How to separate a Tap from a Swipe? 0 Answers