How can i move a 2d player in a 3d scene in x and z axis? maybe with a canvas joystick?
This is the player controller script
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
public bool grounded;
private Rigidbody2D rb2D;
private Animator anim;
void Start ()
{
rb2D = gameObject.GetComponent<Rigidbody2D> ();
anim = gameObject.GetComponent<Animator> ();
}
void Update ()
{
anim.SetBool ("Grounded", grounded);
anim.SetFloat ("Speed", Mathf.Abs (Input.GetAxis ("Horizontal")));
if (Input.GetAxis ("Horizontal") < -0.1f)
{
transform.localScale = new Vector3 (-1, 1, 1);
}
if (Input.GetAxis ("Horizontal") > 0.1f)
{
transform.localScale = new Vector3 (1, 1, 1);
}
if (Input.GetButtonDown ("Jump") && grounded)
{
rb2D.AddForce (Vector2.up * jumpPower);
}
}
void FixedUpdate()
{
float h = Input.GetAxis ("Horizontal");
//Moving the player
rb2D.AddForce ((Vector2.right * speed) * h);
//Limiting the speed of the player
if (rb2D.velocity.x > maxSpeed)
{
rb2D.velocity = new Vector2 (maxSpeed, rb2D.velocity.y);
}
if (rb2D.velocity.x < -maxSpeed)
{
rb2D.velocity = new Vector2 (-maxSpeed, rb2D.velocity.y);
}
}
}