How can i move a 2d player in a 3d scene in x and z axis? maybe with a canvas joystick?
How can i move a 2d player in a 3d scene in x and z axis? maybe with a canvas joystick?
This is the provisory Touchcontrols script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TouchControls : $$anonymous$$onoBehaviour {
private PlayerController thePlayer;
// Use this for initialization
void Start () {
thePlayer = FindObjectOfType<PlayerController> ();
}
public void LeftArrow()
{
transform.localScale = new Vector3 (-1, 1, 1);
//thePlayer.$$anonymous$$ove (-1);
}
public void RightArrow()
{
transform.localScale = new Vector3 (1, 1, 1);
//thePlayer.$$anonymous$$ove (1);
}
public void UnPressedArrow()
{
transform.localScale = new Vector3 (0, 0, 0);
//thePlayer.$$anonymous$$ove (1):
}
}
Answer by QuentinJ724 · Mar 15, 2017 at 06:57 PM
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);
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Sprite Pivot Points 0 Answers
Unity 3D #C Nightvision does not want to switch on or off. 0 Answers
X360 Controller detection gives uneven quadrants 0 Answers
How to check if player is rapidly rotating the joystick? 2 Answers
Joystick freezes when new scene is loaded,Joystick freezes when loading a new scene 0 Answers