Question by
veerlade · Feb 21, 2016 at 04:21 PM ·
controllerjoystickcontrollersjoysticks
I need to control my player using a joystick. I used the default joystick script in Unity, but i am not able to integrate it with the player. Please help. I am attaching the player script, its currently using keyboard input
using UnityEngine; using System.Collections; using UnityStandardAssets.CrossPlatformInput; public class Player : MonoBehaviour {
// movement
private float moveSpeed = 5;
private int moveDirX;
private int moveDirY;
private Vector3 movement;
private Transform thisTransform;
private float xValAtEntry;
public static bool blockedRight = false;
public static bool blockedLeft = false;
public static bool blockedUp = false;
public static bool blockedDown = false;
public static bool onLadder = false;
public static bool isLeft;
public static bool isRight;
public static bool isUp;
public static bool isDown;
public static float playerHitboxX = 0.225f; // player x = 0.45
public static float playerHitboxY = 0.5f;
public static bool alive;
public static bool onTop = false;
public static float orthSize;
public static float orthSizeX;
public static float orthSizeY;
public static float camRatio;
private Vector3 ladderHitbox;
public static int facingDir = 1; // 1 = left, 2 = right, 3 = up, 4 = down
public enum anim { None, WalkLeft, WalkRight, RopeLeft, RopeRight, Climb, ClimbStop, StandLeft, StandRight, HangLeft, HangRight, FallLeft, FallRight }
public static Vector3 glx;
void Awake()
{
thisTransform = transform;
}
void Start()
{
alive = true;
}
/* ============================== CONTROLS ============================== */
public void Update ()
{
// keyboard input
if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow) ) {
isLeft = true;
}
if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow) ) {
isRight = true;
}
if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.UpArrow)) {
isUp = true;
}
if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow)) {
isDown = true;
}
moveDirX = 0;
moveDirY = 0;
// move left
if(isLeft && !blockedLeft)
{
moveDirX = -1;
}
// move right
if(isRight && !blockedRight)
{
moveDirX = 1;
}
// move up on ladder
if(isUp && !blockedUp && onLadder)
{
moveDirY = 1;
}
// move down on ladder
if(isDown && !blockedDown && onLadder)
{
moveDirY = -1;
}
movement = new Vector3(moveDirX, moveDirY,0f) * Time.deltaTime*moveSpeed;
thisTransform.Translate(movement.x,movement.y, 0f);
}
/* ============================== TRIGGER EVENTS ====================================================================== */
void OnTriggerEnter(Collider other)
{
xValAtEntry = thisTransform.position.y;
onLadder = true;
}
void OnTriggerStay(Collider other)
{
// is the player overlapping a ladder?
if(other.gameObject.CompareTag("Ladder"))
{
onLadder = true;
blockedUp = false;
blockedDown = false;
if ( thisTransform.position.y > xValAtEntry)
{
blockedLeft = true;
blockedRight = true;
}
if (!onTop && thisTransform.position.y < xValAtEntry) {
blockedLeft = true;
blockedRight = true;
}
ladderHitbox.y = other.transform.localScale.y * 0.5f; // get half the ladders Y height
// is the player overlapping the ladder?
// if player is landing on top of ladder from a fall, let him pass by
if ((thisTransform.position.y + Player.playerHitboxY) < ((ladderHitbox.y + 0.1f) + other.transform.position.y))
{
onLadder = true;
}
// if the player is at the top of the ladder, then snap her to the top
if ((thisTransform.position.y + Player.playerHitboxY) >= (ladderHitbox.y + other.transform.position.y) && isUp) {
blockedUp = true;
Player.glx = thisTransform.position;
Player.glx.y = (ladderHitbox.y + other.transform.position.y) - Player.playerHitboxY;
thisTransform.position = Player.glx;
onTop = true;
blockedLeft = false;
blockedRight = false;
xValAtEntry = thisTransform.position.y;
} else {
onTop = false;
}
// if the player is at the bottom of the ladder, then snap her to the bottom
if ((thisTransform.position.y - Player.playerHitboxY) <= (-ladderHitbox.y + other.transform.position.y))
{
blockedDown = true;
Player.glx = thisTransform.position;
Player.glx.y = (-ladderHitbox.y + other.transform.position.y) + Player.playerHitboxY;
thisTransform.position = Player.glx;
blockedLeft = false;
blockedRight = false;
}
}
}
void OnTriggerExit(Collider other)
{
// did the player exit a ladder trigger?
if (other.gameObject.CompareTag("Ladder"))
{
onLadder = false;
blockedLeft = false;
blockedRight = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
how to map joystick for mobile game? 0 Answers
Controller Stick Input "Snaps" 0 Answers
How can I listen for input from all joystick axes through c sharp code? 0 Answers
Multi Controller Support for UI 0 Answers
bluetooth AR gun with android 0 Answers