Search Unity

How do I use the rear touch pad?

Discussion in 'PSM' started by SoloWanderer, Feb 8, 2015.

  1. SoloWanderer

    SoloWanderer

    Joined:
    Jan 28, 2015
    Posts:
    20
    How do I use the rear touch pad? I want to use it so it'll work in place of the mouse in this script:

    using UnityEngine;
    using System.Collections;

    public class Rotate: MonoBehaviour {

    public int rotationOffset = 90;

    void Update () {

    Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
    difference.Normalize ();

    float rotZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotationOffset);
    }
    }
     
  2. blackbird

    blackbird

    Joined:
    Aug 9, 2011
    Posts:
    591
    The rear touch pad is implemented through the UnityEngine.PSM.PSMInput class. For example, to iterate through each of current touch you would use the code:


    using UnityEngine.PSM;
    void OnGUI () {
    foreach (Touch touch in PSMInput.touchesSecondary)
    {
    Vector2 pos = touch.position;
    GUI.Label(new Rect(pos.x, Screen.height - pos.y, 50, 30), "(X) #" + touch.fingerId);
    }
    }
     
  3. SoloWanderer

    SoloWanderer

    Joined:
    Jan 28, 2015
    Posts:
    20
    Thanks for the help, but because I'm a C# noob, I don't know how to use this in place of the mouse for my script.
     
  4. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    Maybe try something like this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.PSM;
    5.  
    6.  
    7. public class Rotate : MonoBehaviour
    8. {
    9.  
    10.     public int rotationOffset = 90;
    11.  
    12.     void Update()
    13.     {
    14.         foreach (Touch touch in PSMInput.touchesSecondary)
    15.         {
    16.             if (touch.fingerId == 0)
    17.             {
    18.                 Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    19.                 difference.Normalize();
    20.  
    21.                 float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    22.                 transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotationOffset);
    23.             }
    24.         }
    25.     }
    26. }
     
  5. SoloWanderer

    SoloWanderer

    Joined:
    Jan 28, 2015
    Posts:
    20
    Thanks, DanielSnd!
    I have one last question, but it's so small and easy that I don't think it would be a good idea to create a new thread for it. I want to know how I can use the right joystick for my rotation script. I figured out why almost no games (I've played) use the rear touch pad.
     
  6. DanielSnd

    DanielSnd

    Joined:
    Sep 4, 2013
    Posts:
    382
    Set those on your Input Manager


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.PSM;
    5.  
    6.  
    7. public class Rotate : MonoBehaviour
    8. {
    9.  
    10.     public int rotationOffset = 90;
    11.  
    12.     void Update()
    13.     {
    14.         // See InputManager (Edit > Project > Input) for mapping from <string> to <axis>
    15.         float x = Input.GetAxis("RightStickH");
    16.         float y = Input.GetAxis("RightStickV");
    17.  
    18.         Vector3 difference = new Vector3(x, y, 0);
    19.  
    20.         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    21.         transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotationOffset);
    22.     }
    23. }
     
  7. SoloWanderer

    SoloWanderer

    Joined:
    Jan 28, 2015
    Posts:
    20
    Thank you very much! I appreciate the quick response also!