• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how to map joystick for mobile game? 0 Answers

How can I listen for input from all joystick axes through c sharp code? 0 Answers

Controller Stick Input "Snaps" 0 Answers

Charachter goes trough everything even the terrain :( 1 Answer

Integrating Virtual Joystick to the standart ThirdPersonController. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges