Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges