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 /
avatar image
0
Question by kovachx · Nov 17, 2015 at 02:29 PM · androidunity 5programming

Adding Android Mobile Controller to Unity Project

have been trying for quiet some time now to find out how can I place an android mobile game controller in Unity. The game I have is using the mouse and keyboard buttons to do that but because I want to publish it on Android as well I want to add mobile controllers and I am not very familiar with that.

Here is my PlayerController.cs: I have been trying for quiet some time now to find out how can I place an android mobile game controller in Unity. The game I have is using the mouse and keyboard buttons to do that but because I want to publish it on Android as well I want to add mobile controllers and I am not very familiar with that.

Here is my PlayerController.cs:

 public class PlayerControllerUnity : MonoBehaviour
 {
 public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
 public RotationAxes axes = RotationAxes.MouseXAndY;

 public float sensitivityX = 15.0f;
 public float sensitivityY = 15.0f;

 public float minimumX = -360.0f;
 public float maximumX = 360.0f;

 public float minimumY = -89.0f;
 public float maximumY = 89.0f;

 private float rotationYaxis = 0.0f;
 private float rotationXaxis = 0.0f;

 private Quaternion originalCameraRotation;
 private Quaternion originalPlayerRotation;

 public PlayerUnity playerUnity;

 private GameObject hand;
 
 private GameObject goInHand;
 private CWObject currentObjectInHand;

 private Vector3 positionHand_Tile = new Vector3(0.15f, -0.15f, 0.3f);
 private Vector3 scaleHand_Tile = new Vector3(0.1f, 0.1f, 0.1f);
 private Quaternion rotationHand_Tile = Quaternion.Euler(-15.0f, 0.0f, 15.0f);

 private Vector3 positionHand_Item = new Vector3(0.15f, -0.15f, 0.2f);
 private Vector3 scaleHand_Item = new Vector3(0.1f, 0.1f, 0.1f);
 private Quaternion rotationHand_Item = Quaternion.Euler(0.0f, 55.0f, 0.0f);

 private Vector3 positionHand_Current;
 private Vector3 scaleHand_Current;
 private Quaternion rotationHand_Current;

 private bool firstUpdate = true;

 public void Start()
 {
     originalCameraRotation = playerUnity.mainCamera.transform.localRotation;
     originalPlayerRotation = transform.localRotation;
     
     hand = new GameObject();
     hand.name = "Hand";

     hand.transform.parent = playerUnity.mainCamera.transform;

     hand.transform.localPosition = positionHand_Tile;
     hand.transform.localScale = scaleHand_Tile;
     hand.transform.localRotation = rotationHand_Tile;
 }

 public void UpdateControlled()
 {
     if (playerUnity.gameManagerUnity.State == GameManagerUnity.GameManagerUnityState.GAME &&
         playerUnity.playerGUI.ActiveState == PlayerGUI.State.NORMAL)
     {
         if (Screen.lockCursor == false)
         {
             //Auto pause if the user leaves the game for some reason (ALT+TAB, etc..)
             playerUnity.gameManagerUnity.Pause();
         }
         else
         {
             if (firstUpdate)
             {
                 rotationYaxis = playerUnity.player.rotation.y;
                 rotationXaxis = playerUnity.player.rotation.x;
                 firstUpdate = false;
             }

             if (Input.GetKeyDown(KeyCode.R))
                 playerUnity.player.ResetPosition();

             if (Input.GetKeyDown(KeyCode.C))
                 playerUnity.ChangeCamera();

             UpdateJump();
             UpdateMovement();
             UpdateCameraRotation();
             UpdateUserActions();
             UpdateItemOnHand();

             playerUnity.player.rotation.y = rotationYaxis;
             playerUnity.player.rotation.x = rotationXaxis;
         }
     }
 }

 private void ExecuteHandUseAnimation()
 {
     handUseAnimationTimer = 0.5f;
 }
 
 private float handUseAnimationTimer;
 private float handMovementTimer;

 private void UpdateItemOnHand()
 {
     if (currentObjectInHand != playerUnity.objectInHand)
     {
         if (goInHand)
         {
             playerUnity.gameManagerUnity.objectsManagerUnity.RemoveGameObject(goInHand);
             
             goInHand = null;
         }
         
         this.currentObjectInHand = playerUnity.objectInHand;
         
         if (currentObjectInHand != null)
         {
             goInHand = playerUnity.gameManagerUnity.objectsManagerUnity.CreateGameObjectFromObject(currentObjectInHand);
             
             goInHand.transform.parent = hand.transform;
             goInHand.transform.localScale = new Vector3(1, 1, 1);
             goInHand.transform.localPosition = new Vector3(0, 0, 0);
             goInHand.transform.localRotation = Quaternion.identity;

             switch (currentObjectInHand.definition.type)
             {
                 case CWDefinition.DefinitionType.Item:
                     positionHand_Current = positionHand_Item;
                     scaleHand_Current = scaleHand_Item;
                     rotationHand_Current = rotationHand_Item;
                     break;

                 case CWDefinition.DefinitionType.Tile:
                     positionHand_Current = positionHand_Tile;
                     scaleHand_Current = scaleHand_Tile;
                     rotationHand_Current = rotationHand_Tile;
                     break;
             }

             hand.transform.localPosition = positionHand_Current;
             hand.transform.localScale = scaleHand_Current;
             hand.transform.localRotation = rotationHand_Current;
         }
     }
     
     if (handUseAnimationTimer <= 0.0f)
     {
         if (playerUnity.player.input.moveDirection.magnitude > 0.0f)
         {
             handMovementTimer += Time.deltaTime;
 
             float deltaY = Mathf.Sin(handMovementTimer * 10) * 0.02f;
             float deltaX = Mathf.Sin(handMovementTimer * 10) * 0.01f;
 
             hand.transform.localPosition = positionHand_Current + new Vector3(deltaX, deltaY, 0.0f);
         }
         else
         {
             handMovementTimer = 0.0f;
             hand.transform.localPosition = positionHand_Current;
         }
     }
     else
     {
         if (currentObjectInHand != null)
         {
             float deltaRotation = Mathf.Sin(handUseAnimationTimer * 2.0f * Mathf.PI) * 30;

             hand.transform.localPosition = positionHand_Current;

             switch (currentObjectInHand.definition.type)
             {
                 case CWDefinition.DefinitionType.Tile:
                     hand.transform.localRotation = rotationHand_Current * Quaternion.Euler(deltaRotation, 0, 0);
                     break;

                 case CWDefinition.DefinitionType.Item:
                     hand.transform.localRotation = rotationHand_Current * Quaternion.Euler(0, 0, deltaRotation);
                     break;
             }
         }
     
         handUseAnimationTimer -= Time.deltaTime;
         
         if (handUseAnimationTimer <= 0.0f)
         {
             hand.transform.localRotation = rotationHand_Current;
             handUseAnimationTimer = 0.0f;
         }
     }
 }
 
 private float userActionCooldown;

 private void UpdateUserActions()
 {
     if (playerUnity.gameManagerUnity.State == GameManagerUnity.GameManagerUnityState.GAME ||
         playerUnity.gameManagerUnity.State == GameManagerUnity.GameManagerUnityState.PAUSE)
     {
         Vector3 cameraPos = playerUnity.transform.position + playerUnity.GetLocalHeadPosition();
         Vector3 cameraFwd = playerUnity.mainCamera.transform.forward;

         CubeWorld.Utils.Graphics.RaycastTileResult raycastResult = CubeWorld.Utils.Graphics.RaycastTile(
                                                     playerUnity.player.world,
                                                     GraphicsUnity.Vector3ToCubeWorldVector3(cameraPos),
                                                     GraphicsUnity.Vector3ToCubeWorldVector3(cameraFwd),
                                                     10.0f,
                                                     true, false);

         if (userActionCooldown > 0.0f)
             userActionCooldown -= Time.deltaTime;
         
         if (userActionCooldown <= 0.0f)
         {
             if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
             {
                 ExecuteHandUseAnimation();

                 userActionCooldown = 0.2f;
             }

             if (raycastResult.hit)
             {
                 if (Input.GetMouseButton(0))
                 {
                     if (raycastResult.position.x > 0 && raycastResult.position.x < playerUnity.player.world.tileManager.sizeX - 1 &&
                         raycastResult.position.z > 0 && raycastResult.position.z < playerUnity.player.world.tileManager.sizeZ - 1 &&
                         raycastResult.position.y > 0)
                     {
                         if (playerUnity.player.world.tileManager.HasTileActions(
                             raycastResult.position,
                             TileActionRule.ActionType.CLICKED))
                         {
                             playerUnity.player.world.gameplay.TileClicked(raycastResult.position);
                         }
                         else
                         {
                             if (playerUnity.objectInHand != null)
                             {
                                 switch (playerUnity.objectInHand.definition.type)
                                 {
                                     case CWDefinition.DefinitionType.Item:
                                     {
                                         playerUnity.gameManagerUnity.fxManagerUnity.PlaySound("hitmetal", playerUnity.player.position);
                                         playerUnity.player.world.gameplay.TileHit(raycastResult.position, ((Item)playerUnity.objectInHand).itemDefinition);
                                         break;
                                     }

                                     default:
                                         playerUnity.gameManagerUnity.fxManagerUnity.PlaySound("hit", playerUnity.player.position);
                                         playerUnity.player.world.tileManager.DamageTile(raycastResult.position, 1);
                                         break;
                                 }

                             }
                         }
                     }
                 }
                 else if (Input.GetMouseButton(1))
                 {
                     if (playerUnity.objectInHand != null && playerUnity.objectInHand.definition.type == CWDefinition.DefinitionType.Tile)
                     {
                         TileDefinition tileDefinition = (TileDefinition) playerUnity.objectInHand.definition;
                         TilePosition tileCreatePosition = raycastResult.position + CubeWorld.Utils.Graphics.GetFaceNormal(raycastResult.face);

                         //Don't create tile on top of the world, because no triangles are drawn on the border!
                         if (tileCreatePosition.y < playerUnity.player.world.tileManager.sizeY - 1 &&
                             playerUnity.player.world.tileManager.IsValidTile(tileCreatePosition) &&
                             playerUnity.player.world.tileManager.GetTileSolid(tileCreatePosition) == false)
                         {
                             if (playerUnity.player.world.avatarManager.IsTileBlockedByAnyAvatar(tileCreatePosition) == false)
                             {
                                 playerUnity.player.world.gameplay.CreateTile(tileCreatePosition, tileDefinition.tileType);

                                 playerUnity.player.inventory.RemoveFromDefinition(tileDefinition, 1);

                                 if (playerUnity.player.inventory.HasMoreOfDefinition(tileDefinition) == false)
                                     playerUnity.objectInHand = null;
                             }
                         }
                     }
                 }
             }
         }
     }
 }

 private void UpdateJump()
 {
     playerUnity.player.input.jump = Input.GetKey(KeyCode.Space);
 }
 
 private void UpdateMovement()
 {
     float h = Input.GetAxisRaw("Horizontal");
     float v = Input.GetAxisRaw("Vertical");

     Vector3 dirWalk = transform.forward * v;
     Vector3 dirStrafe = transform.right * h;
     Vector3 dir = dirWalk + dirStrafe;
     dir.y = 0;
     dir.Normalize();

     playerUnity.player.input.moveDirection = GraphicsUnity.Vector3ToCubeWorldVector3(dir);
 }

 private void UpdateCameraRotation()
 {
     if (Screen.lockCursor)
     {
         if (axes == RotationAxes.MouseXAndY)
         {
             // Read the mouse input axis
             rotationYaxis += Input.GetAxis("Mouse X") * sensitivityX;
             rotationXaxis += Input.GetAxis("Mouse Y") * sensitivityY;

             rotationYaxis = ClampAngle(rotationYaxis, minimumX, maximumX);
             rotationXaxis = ClampAngle(rotationXaxis, minimumY, maximumY);

             Quaternion xQuaternion = Quaternion.AngleAxis(rotationYaxis, Vector3.up);
             Quaternion yQuaternion = Quaternion.AngleAxis(rotationXaxis, Vector3.left);

             playerUnity.mainCamera.transform.localRotation = originalCameraRotation * yQuaternion;
             transform.localRotation = originalPlayerRotation * xQuaternion;
         }
         else if (axes == RotationAxes.MouseX)
         {
             rotationYaxis += Input.GetAxis("Mouse X") * sensitivityX;
             rotationYaxis = ClampAngle(rotationYaxis, minimumX, maximumX);

             Quaternion xQuaternion = Quaternion.AngleAxis(rotationYaxis, Vector3.up);
             transform.localRotation = originalPlayerRotation * xQuaternion;
         }
         else
         {
             rotationXaxis += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationXaxis = ClampAngle(rotationXaxis, minimumY, maximumY);

             Quaternion yQuaternion = Quaternion.AngleAxis(rotationXaxis, Vector3.left);
             playerUnity.mainCamera.transform.localRotation = originalCameraRotation * yQuaternion;
         }
     }
 }

 public static float ClampAngle(float angle, float min, float max)
 {
     if (angle < -360F)
         angle += 360F;
     if (angle > 360F)
         angle -= 360F;
     return Mathf.Clamp(angle, min, max);
 }

}

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

Multiple Cars not working 1 Answer

'Failed to create agent' error made even after building the NavMesh in real time - AR Foundation 0 Answers

How to move a player automatically through a List of Waypoints? 1 Answer

Why doesn't my game update on Android Tablet unless I'm clicking the screen? 1 Answer

Camera follow jitter when player move back on platform 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