- Home /
Question by
lessthanthreewizard · Oct 31, 2019 at 07:47 AM ·
2d gamemovement scripttouch controls
Touch controls no longer work on movement script,2D movement script not working with mobile touch
Hey so I've been testing free joystick assets from the asset store with a movement script I wrote following a tutorial, The joysticks were working fine on my mobile with a previous movement script without having to edit anything; But now with this new script they completly stopped working, I tried creating new inputs in the input manager and assigning them to the joystick and to the script but that didn't work, Heres the script I'm using now
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Playercontroller : MonoBehaviour { public Vector2 movementDirection; public float movementSpeed; public Rigidbody2D rb; public Animator animator; public Joystick joystick;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
ProcessInputs();
Move();
Animate();
void ProcessInputs()
{
movementDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
movementSpeed = Mathf.Clamp(movementDirection.magnitude, 0.0f, 1.0f);
movementDirection.Normalize();
}
void Move()
{
rb.velocity = movementDirection * movementSpeed;
}
void Animate() {
if (movementDirection != Vector2.zero)
{
animator.SetFloat("Horizontal", movementDirection.x);
animator.SetFloat("Vertical", movementDirection.y);
}
animator.SetFloat("Speed", movementSpeed);
{
}
}
}
}
Comment