Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to convert desktop app to smartphone app?

Discussion in 'Editor & General Support' started by Deleted User, Nov 11, 2019.

  1. Deleted User

    Deleted User

    Guest

    I completed an Udemy course, followed by many basic Unity courses. I am now in the process of making minor improvements to my udemy projects to better understandd how things work. I have just succeeded enabling my paddle to move left & right given some basic code modified from another 3G tutorial. It was originally given by the unity development team. However, upon adding these codes, the paddle either moves too slowly or extremely fast left or right. How can I set it so it moves slowly and smoothly? I have already tried by changing the speed (Max X variable) to as high as 30 and as low as 4 and it didn't work. When it was set to 30, it moves ridiculously fast left and right. My screenshot is below so you know which variable I changed and the code is only for 1 file is as below. If anyone needs the entire project, please ask. Thanks.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PaddleScript : MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.     public float speed;
    9.     public float maxX;
    10.     public float ballForce;
    11.     public bool gameStarted;
    12.  
    13.     Vector3 position;
    14.  
    15.     bool currentPlatformAndroid = true;
    16.  
    17.     // Start is called before the first frame update
    18.  
    19.  
    20.    
    21.     void Awake()
    22.     {
    23.         rb = GetComponent<Rigidbody2D>();
    24.  
    25.         #if UNITY_ANDROID
    26.             currentPlatformAndroid = true;
    27.         #else
    28.             currentPlatformAndroid = false;
    29.         #endif
    30.  
    31.     }
    32.    
    33.  
    34.  
    35.  
    36.     void Start()
    37.     {
    38.         position = transform.position;
    39.  
    40.         if (currentPlatformAndroid == true)
    41.         {
    42.             Debug.Log("Android");
    43.  
    44.         }
    45.  
    46.         else
    47.  
    48.         {
    49.             Debug.Log("Windows");
    50.  
    51.         }
    52.  
    53.     }
    54.  
    55.     // Update is called once per frame
    56.  
    57.  
    58.     void Update()
    59.     {
    60.         // original settings for Windows is max X = 2.4
    61.         // speed is 5
    62.  
    63.  
    64.  
    65.         float x = Input.GetAxis("Horizontal");
    66.  
    67.         if (x < 0)
    68.         {
    69.             MoveLeft();
    70.  
    71.         }
    72.  
    73.         if (x > 0)
    74.         {
    75.             MoveRight();
    76.  
    77.         }
    78.  
    79.         if (x == 0)
    80.         {
    81.             Stop();
    82.  
    83.         }
    84.  
    85.         Vector3 pos = transform.position;
    86.         pos.x = Mathf.Clamp(pos.x,-maxX,maxX);
    87.         transform.position = pos;
    88.  
    89.     }
    90.  
    91.     void FixedUpdate()
    92.     {
    93.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    94.         {
    95.  
    96.             Vector4 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
    97.             //Vector3 movement2 = new Vector4(touchDeltaPosition.x, 0.0f, touchDeltaPosition.y);
    98.  
    99.             Vector4 movement2 = new Vector4(touchDeltaPosition.x, touchDeltaPosition.y);
    100.  
    101.             rb.AddForce(movement2 * maxX);
    102.             gameStarted = true;
    103.  
    104.         }
    105.  
    106.     }
    107.    
    108.     void TouchMove()
    109.     {
    110.         if (Input.touchCount > 0)
    111.         {
    112.             Touch touch = Input.GetTouch(0);
    113.             float middle = Screen.width / 2;
    114.             if (touch.position.x < middle && touch.phase == TouchPhase.Began)
    115.             {
    116.                 MoveLeft();
    117.  
    118.             }
    119.  
    120.             if (touch.position.x > middle && touch.phase == TouchPhase.Began)
    121.             {
    122.                 MoveRight();
    123.  
    124.             }
    125.  
    126.         }
    127.  
    128.         else
    129.  
    130.         {
    131.             Stop();
    132.         }
    133.        
    134. }
    135.  
    136.  
    137.     public void MoveLeft()
    138.     {
    139.         rb.velocity = new Vector2(-speed, 0);
    140.  
    141.     }
    142.  
    143.     public void MoveRight()
    144.     {
    145.         rb.velocity = new Vector2(speed,0);
    146.  
    147.  
    148.     }
    149.  
    150.     public void Stop()
    151.     {
    152.         rb.velocity = Vector2.zero;
    153.     }
    154. }
     

    Attached Files:

unityunity