Hello
I have a script for my player’s movement but for some reason it only works on PC. I tried SO many methods to make the player move on mobile (including joysticks, Raycasters and CrossPlatform) but nothing works. Here is my current script, would love if anybody could help me figure out why it won’t work:
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine;
public class Movement : MonoBehaviour
{
//variables
public Animator animator;
public float moveSpeed = 0.9f;
public GameObject character;
private Rigidbody2D characterBody;
private float ScreenWidth;
// Use this for initialization
void Start()
{
gameObject.tag = "Player";
ScreenWidth = Screen.width;
characterBody = character.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
int i = 0;
//loop over every touch found
while (i < Input.touchCount)
{
if (Input.GetTouch(i).position.x > ScreenWidth / 2)
{
//move right
RunCharacter(0f);
}
if (Input.GetTouch(i).position.x < ScreenWidth / 2)
{
//move left
RunCharacter(0f);
}
++i;
}
}
void FixedUpdate()
{
animator.SetFloat("Horizontal", CrossPlatformInputManager.GetAxis("Horizontal"));
#if UNITY_EDITOR
RunCharacter(Input.GetAxis("Horizontal"));
#endif
}
private void RunCharacter(float horizontalInput)
{
//move player
//characterBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0f));
characterBody.velocity = new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0.0f);
//animator
if (CrossPlatformInputManager.GetAxisRaw("Horizontal") == 1 || CrossPlatformInputManager.GetAxisRaw("Horizontal") == -1)
{
animator.SetFloat("LastMove", CrossPlatformInputManager.GetAxisRaw("Horizontal"));
}
}
}
also note: this script used to work on my early drafts. Not sure what changed since I only added an animator to it.