- Home /
 
 
               Question by 
               unity_PM96A4-pjeC-4Q · Nov 30, 2017 at 05:15 PM · 
                c#2d gamemobilecontrolscrossplatform  
              
 
              Making 2d joystick correct to controls (mobile)
So I have my controls that works pretty good for me on PC, but I need them on mobile too. So I added a joystick from regular unity assets. But, joystick movement is something else :) I need the ship to turn first and then accelerate to the side that the joystick deviated.
my controls:
 using System;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using UnityStandardAssets.CrossPlatformInput;
 
 
 public class PlayerMovement : MonoBehaviour
 {
     public float verticalInputAcceleration = 1;
     public float horizontalInputAcceleration = 20;
 
     public float maxSpeed = 10;
     public float maxRotationSpeed = 100;
 
     public float velocityDrag = 1;
     public float rotationDrag = 1;
 
     private Vector3 velocity;
     private float zRotationVelocity;
 
     private void Update()
     {
         // apply forward input
         Vector3 acceleration = CrossPlatformInputManager.GetAxis("Vertical") * verticalInputAcceleration * transform.up;
         velocity += acceleration * Time.deltaTime;
 
         // apply turn input
         float zTurnAcceleration = -1 * CrossPlatformInputManager.GetAxis("Horizontal") * horizontalInputAcceleration;
         zRotationVelocity += zTurnAcceleration * Time.deltaTime;
     }
 
     private void FixedUpdate()
     {
         // apply velocity drag
         velocity = velocity * (1 - Time.deltaTime * velocityDrag);
 
         // clamp to maxSpeed
         velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
 
         // apply rotation drag
         zRotationVelocity = zRotationVelocity * (1 - Time.deltaTime * rotationDrag);
 
         // clamp to maxRotationSpeed
         zRotationVelocity = Mathf.Clamp(zRotationVelocity, -maxRotationSpeed, maxRotationSpeed);
 
         // update transform
         transform.position += velocity * Time.deltaTime;
         transform.Rotate(0, 0, zRotationVelocity * Time.deltaTime);
     }
 }
 
               Joystick scrips : \
 using System;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 namespace UnityStandardAssets.CrossPlatformInput
 {
     public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
     {
         public enum AxisOption
         {
             // Options for which axes to use
             Both, // Use both
             OnlyHorizontal, // Only horizontal
             OnlyVertical // Only vertical
         }
 
         public int MovementRange = 100;
         public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
         public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
         public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
 
         Vector3 m_StartPos;
         bool m_UseX; // Toggle for using the x axis
         bool m_UseY; // Toggle for using the Y axis
         CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
         CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
 
         void OnEnable()
         {
             CreateVirtualAxes();
         }
 
         void Start()
         {
             m_StartPos = transform.position;
         }
 
         void UpdateVirtualAxes(Vector3 value)
         {
             var delta = m_StartPos - value;
             delta.y = -delta.y;
             delta /= MovementRange;
             if (m_UseX)
             {
                 m_HorizontalVirtualAxis.Update(-delta.x);
             }
 
             if (m_UseY)
             {
                 m_VerticalVirtualAxis.Update(delta.y);
             }
         }
 
         void CreateVirtualAxes()
         {
             // set axes to use
             m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
             m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
 
             // create new axes based on axes to use
             if (m_UseX)
             {
                 m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
                 CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
             }
             if (m_UseY)
             {
                 m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
                 CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
             }
         }
 
 
         public void OnDrag(PointerEventData data)
         {
             Vector3 newPos = Vector3.zero;
 
             if (m_UseX)
             {
                 int delta = (int)(data.position.x - m_StartPos.x);
                 delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
                 newPos.x = delta;
             }
 
             if (m_UseY)
             {
                 int delta = (int)(data.position.y - m_StartPos.y);
                 delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
                 newPos.y = delta;
             }
             transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
             UpdateVirtualAxes(transform.position);
         }
 
 
         public void OnPointerUp(PointerEventData data)
         {
             transform.position = m_StartPos;
             UpdateVirtualAxes(m_StartPos);
         }
 
 
         public void OnPointerDown(PointerEventData data) { }
 
         void OnDisable()
         {
             // remove the joysticks from the cross platform input
             if (m_UseX)
             {
                 m_HorizontalVirtualAxis.Remove();
             }
             if (m_UseY)
             {
                 m_VerticalVirtualAxis.Remove();
             }
         }
     }
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Cross Platform Input touchpad acceleration 1 Answer
Android Sliding Controls 0 Answers
Move 2D Sprite Horizontally With Mouse 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers