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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issue With Mobile Joysticks

Discussion in 'Scripting' started by DarkLord987, Jun 12, 2015.

  1. DarkLord987

    DarkLord987

    Joined:
    Jan 14, 2014
    Posts:
    21
    I am trying to use the Standard Shooter asset code to work with mobile joysticks but it isn't working. Mobile joysticks did work, but now they don't. Here is the PlayerMovement code so far. If you need any more code to assist me just ask.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnitySampleAssets.CrossPlatformInput;
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public float speed = 6f;            // The speed that the player will move at.
    7.    
    8.     Vector3 movement;                   // The vector to store the direction of the player's movement.
    9.     Animator anim;                      // Reference to the animator component.
    10.     Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    11.     int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    12.     float camRayLength = 100f;          // The length of the ray from the camera into the scene.
    13.    
    14.     void Awake ()
    15.     {
    16.         // Create a layer mask for the floor layer.
    17.         floorMask = LayerMask.GetMask ("Floor");
    18.        
    19.         // Set up references.
    20.         anim = GetComponent <Animator> ();
    21.         playerRigidbody = GetComponent <Rigidbody> ();
    22.     }
    23.    
    24.    
    25.     void FixedUpdate ()
    26.     {
    27.         // Store the input axes.
    28.         float h = CrossPlatformInputManager.GetAxisRaw("Horizontal");
    29.         float v = CrossPlatformInputManager.GetAxisRaw("Vertical");
    30.        
    31.         // Move the player around the scene.
    32.         Move (h, v);
    33.        
    34.         // Turn the player to face the mouse cursor.
    35.         Turning ();
    36.        
    37.         // Animate the player.
    38.         Animating (h, v);
    39.     }
    40.    
    41.     void Move (float h, float v)
    42.     {
    43.         // Set the movement vector based on the axis input.
    44.         movement.Set (h, 0f, v);
    45.        
    46.         // Normalise the movement vector and make it proportional to the speed per second.
    47.         movement = movement.normalized * speed * Time.deltaTime;
    48.        
    49.         // Move the player to it's current position plus the movement.
    50.         playerRigidbody.MovePosition (transform.position + movement);
    51.     }
    52.    
    53.     void Turning ()
    54.     {
    55.         // Create a ray from the mouse cursor on screen in the direction of the camera.
    56.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    57.        
    58.         // Create a RaycastHit variable to store information about what was hit by the ray.
    59.         RaycastHit floorHit;
    60.        
    61.         // Perform the raycast and if it hits something on the floor layer...
    62.         if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    63.         {
    64.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
    65.             Vector3 playerToMouse = floorHit.point - transform.position;
    66.            
    67.             // Ensure the vector is entirely along the floor plane.
    68.             playerToMouse.y = 0f;
    69.            
    70.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
    71.             Quaternion newRotation = Quaternion.LookRotation (movement);
    72.            
    73.             // Set the player's rotation to this new rotation.
    74.             playerRigidbody.MoveRotation (newRotation);
    75.         }
    76.     }
    77.    
    78.     void Animating (float h, float v)
    79.     {
    80.         // Create a boolean that is true if either of the input axes is non-zero.
    81.         bool walking = h != 0f || v != 0f;
    82.        
    83.         // Tell the animator whether or not the player is walking.
    84.         anim.SetBool ("IsWalking", walking);
    85.     }
    86. }
     
  2. DarkLord987

    DarkLord987

    Joined:
    Jan 14, 2014
    Posts:
    21
    Bump. Still no progress fixing it.
     
  3. DarkLord987

    DarkLord987

    Joined:
    Jan 14, 2014
    Posts:
    21
  4. DarkLord987

    DarkLord987

    Joined:
    Jan 14, 2014
    Posts:
    21
  5. Fedryus

    Fedryus

    Joined:
    Mar 2, 2016
    Posts:
    1
    How do you solved it? Pls I am trying to turning my character with a joysticks and I can´t.