Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Windows Phone 8 Touch Controls

Discussion in 'Windows' started by SkyCraw, Sep 19, 2013.

  1. SkyCraw

    SkyCraw

    Joined:
    Sep 18, 2013
    Posts:
    1
    Hello all,

    I've completed the "Stealth" tutorial game with flying colors and tried implementing it on my developer-unlocked HTC. Being fairly new to Unity, I soon discovered that the touch interfaces weren't configured (received no response to anything) after deployment; even after messing with the Input section in Project Settings

    After exploring the forums, I realized it would be possible with the GetTouch() and TouchCount() functions (possibly others I'm unaware about, like for multi-touch). Could anyone show me a sample (for player movement and such) using these functions for a WP8 interface?

    Below is what the tutorial provides for the PlayerMovement script

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public AudioClip shoutingClip;      // Audio clip of the player shouting.
    8.     public float turnSmoothing = 15f;   // A smoothing value for turning the player.
    9.     public float speedDampTime = 0.1f;  // The damping for the speed parameter
    10.    
    11.  
    12.     private Animator anim;              // Reference to the animator component.
    13.     private HashIDs hash;               // Reference to the HashIDs.
    14.    
    15.    
    16.     void Awake ()
    17.     {
    18.         // Setting up the references.
    19.         anim = GetComponent<Animator>();
    20.         hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
    21.        
    22.         // Set the weight of the shouting layer to 1.
    23.         anim.SetLayerWeight(1, 1f);
    24.     }
    25.    
    26.    
    27.     void FixedUpdate ()
    28.     {
    29.         // Cache the inputs.
    30.         float h = Input.GetAxis("Horizontal");
    31.         float v = Input.GetAxis("Vertical");
    32.         bool sneak = Input.GetButton("Sneak");
    33.        
    34.         MovementManagement(h, v, sneak);
    35.     }
    36.    
    37.    
    38.     void Update ()
    39.     {
    40.         // Cache the attention attracting input.
    41.         bool shout = Input.GetButtonDown("Attract");
    42.        
    43.         // Set the animator shouting parameter.
    44.         anim.SetBool(hash.shoutingBool, shout);
    45.        
    46.         AudioManagement(shout);
    47.     }
    48.    
    49.    
    50.     void MovementManagement (float horizontal, float vertical, bool sneaking)
    51.     {
    52.         // Set the sneaking parameter to the sneak input.
    53.         anim.SetBool(hash.sneakingBool, sneaking);
    54.        
    55.         // If there is some axis input...
    56.         if(horizontal != 0f || vertical != 0f)
    57.         {
    58.             // ... set the players rotation and set the speed parameter to 5.5f.
    59.             Rotating(horizontal, vertical);
    60.             anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
    61.         }
    62.         else
    63.             // Otherwise set the speed parameter to 0.
    64.             anim.SetFloat(hash.speedFloat, 0);
    65.     }
    66.    
    67.    
    68.     void Rotating (float horizontal, float vertical)
    69.     {
    70.         // Create a new vector of the horizontal and vertical inputs.
    71.         Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
    72.        
    73.         // Create a rotation based on this new vector assuming that up is the global y axis.
    74.         Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
    75.        
    76.         // Create a rotation that is an increment closer to the target rotation from the player's rotation.
    77.         Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
    78.        
    79.         // Change the players rotation to this new rotation.
    80.         rigidbody.MoveRotation(newRotation);
    81.     }
    82.    
    83.    
    84.     void AudioManagement (bool shout)
    85.     {
    86.         // If the player is currently in the run state...
    87.         if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
    88.         {
    89.             // ... and if the footsteps are not playing...
    90.             if(!audio.isPlaying)
    91.                 // ... play them.
    92.                 audio.Play();
    93.         }
    94.         else
    95.             // Otherwise stop the footsteps.
    96.             audio.Stop();
    97.        
    98.         // If the shout input has been pressed...
    99.         if(shout)
    100.             // ... play the shouting clip where we are.
    101.             AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
    102.     }
    103. }
    104.  
    Thanks a bunch :D