I have been creating a platformer game meant for android for a few days now, due to the fact that I haven’t been able to get android remote to work I have been testing on my PC (the game potentially uses on-screen buttons).
Here is my code for player movement:
using UnityEngine;
using System.Collections;
public class playerMovement1 : MonoBehaviour {
Rect leftButton = new Rect(0, Screen.height-(Screen.height/3), Screen.width/4, Screen.height/3);
Rect rightButton = new Rect(Screen.width/4, Screen.height-(Screen.height/3), Screen.width/4, Screen.height/3);
Rect jumpButton = new Rect(3*(Screen.width/4), Screen.height-(Screen.height/3), Screen.width/4, Screen.height/3);
public float speed;
float xV;
float yV;
float jumpTimeCount = 0;
public float jumpSpeed;
bool left;
bool right;
bool jump;
bool jumping;
bool canJump;
public bool androidBuild;
public bool gravity;
public float gravityForce;
// Use this for initialization
void Start () {
}
void FixedUpdate(){
}
void Update () {
if(jumping){
print ("jumping");
jumpTimeCount += 1*Time.deltaTime;
if(jumpTimeCount>0.8){
//Jumping slows as it reaches peak
yV = jumpSpeed*0.8f;
}
//Upward movement ends when jumpTimCounter is above 1
if(jumpTimeCount>1){
canJump = false;
jumpTimeCount = 0;
jumping = false;
}
}
input ();
tick ();
CharacterController controller = GetComponent<CharacterController>();
if(controller.isGrounded){
print ("grounded");
jumping = false;
canJump = true;
}else{
}
if (controller.collisionFlags == CollisionFlags.Sides){
//Basic wall jump code
print ("touching sides");
canJump = true;
}
if(gravity){
controller.Move(new Vector3(0,gravityForce*Time.deltaTime,0));
}
//All movement performed here
controller.Move(new Vector3(xV*Time.deltaTime,yV*Time.deltaTime,0));
}
void tick(){
if(left){
xV = -speed;
}
if(right){
xV = speed;
}
if(!right && !left){
xV = 0;
}
if(jump){
if(canJump){
jumping = true;
yV = jumpSpeed;
}
}else{
jumping = false;
}
if(!jumping){
yV = 0;
}
}
void input(){
if(Input.touches.Length>0){
for(int i = 0; i<Input.touchCount; i++){
if(jumpButton.Contains(new Vector2(Input.GetTouch(i).position.x, Screen.height-Input.GetTouch(i).position.y))){
jump = true;
}else{
jump = false;
}
if(leftButton.Contains(new Vector2(Input.GetTouch(i).position.x, Screen.height-Input.GetTouch(i).position.y))){
left = true;
}else{
left = false;
}
if(rightButton.Contains(new Vector2(Input.GetTouch(i).position.x, Screen.height-Input.GetTouch(i).position.y))){
right = true;
}else{
right = false;
}
}
}
if(!androidBuild){
if(Input.GetKey(KeyCode.A)){
left = true;
}else{
left = false;
}
if(Input.GetKey(KeyCode.D)){
right = true;
}else{
right = false;
}
if(Input.GetKey(KeyCode.Space)){
jump = true;
}else{
jump = false;
}
}
}
}
When building for android I set androidBuild to true to ensure the Keyboard input code does not interfere.
When I run the game on my phone the movement seems to work differently, you don’t stop when releasing the left or right button and jumping stops all other movement. Is this a problem with my code? or is this usual for android builds, and how would I go about fixing it?
Thanks