I used the space shooter tutorial to create a touch pad for my 2D game where the hero only moves on the horizontal. My 2D character controller uses the same script in the 2D controller tutorial and I am calling Movement from another script. I am kinda of stump on how to get the movement pad to function with get axis. I was also thinking about just getting Control Freak, but want to see if I can get help on solving this before.
This is from the PlayerController
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float maxSpeed = 10.0f;
public bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700.0f;
public JumpButton jumpButton;
public MovementPad movePad;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", GetComponent <Rigidbody2D>().velocity.y);
Vector2 direction = movePad.GetDirection ();
Vector2 movement = new Vector2 (direction.x, 0.0f);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Update ()
{
if(grounded && jumpButton.CanJump())
// if(grounded && Input.GetButtonDown (“Jump”))
{
anim.SetBool (“Ground”, false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
This is the other script that the player controller getting the info.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MovementPad : MonoBehaviour , IPointerDownHandler, IDragHandler, IPointerUpHandler
{
public float smoothing;
private Vector2 origin;
private Vector2 direction;
private Vector2 smoothDirection;
private bool touched;
private int pointerID;
void Awake ()
{
direction = Vector2.zero;
touched = false;
}
public void OnPointerDown (PointerEventData data)
{
// Set our start point
if (!touched)
{
touched = true;
pointerID = data.pointerId;
origin = data.position;
}
}
public void OnDrag (PointerEventData data)
{
//Compare the difference between our start point and current pointer pos
if (data.pointerId == pointerID)
{
Vector2 currentPosition = data.position;
Vector2 directionRaw = currentPosition - origin;
direction = directionRaw.normalized;
}
}
public void OnPointerUp (PointerEventData data)
{
// Reset Everything
if (data.pointerId == pointerID)
{
direction = Vector2.zero;
touched = false;
}
}
public Vector2 GetDirection ()
{
smoothDirection = Vector2.MoveTowards (smoothDirection, direction, smoothing);
return smoothDirection;
}
}