Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Please help me how to maintain the direction and forward value of mobile joystick.

Discussion in 'Scripting' started by WENKO, May 18, 2024.

  1. WENKO

    WENKO

    Joined:
    Apr 27, 2019
    Posts:
    53
    I downloaded a fixed joystick from the Asset Store and tried to move the player in all directions with my fingers and thumb on the 2D screen using the x- and z-axes as horizontal and vertical in 3D.

    When you lift your finger, the joystick automatically centers and stops the player.

    Here, I want to maintain the rotation and forward speed in the last direction before holding the hand, so that even if the player removes the finger and the joystick is automatically returned to the center, the player continues to maintain the state before removing the finger. please help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Windows;
    5.  
    6. public class managerController : MonoBehaviour
    7. {
    8.     public FixedJoystick joystick;
    9.     public Canvas inputCanvas;
    10.     public bool isJoystick;
    11.  
    12.     public GameObject player;
    13.     public GameObject character;
    14.  
    15.     public managerController inputs;
    16.     public Vector3 movementDirection;
    17.     private Vector3 movement;
    18.  
    19.     public float moveSpeed;
    20.     public float rotationSpeed;
    21.     private Vector3 movement;
    22.     private float resultSpeed;
    23.     private float moveX;
    24.     private float moveZ;
    25.  
    26.     public void EnableJoystickInput()
    27.     {
    28.         isJoystick = true;
    29.         inputCanvas.gameObject.SetActive(true);      
    30.    }
    31.  
    32.     // Start is called before the first frame update
    33.  
    34.     void Start()
    35.     {
    36.         EnableJoystickInput();
    37.         resultSpeed = 0.1f * moveSpeed * Time.deltaTime;
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.         if(isJoystick)
    44.         {
    45.             // joystick recognition
    46.             var movementDirection = new Vector3(joystick.Direction.x, y: 0.0f, z:joystick.Direction.y);
    47.  
    48.             if (movementDirection.sqrMagnitude<=0)
    49.             {
    50.                 return;
    51.             }
    52.  
    53.             // Rotation of characters; Only the plane turns in the direction it is going
    54.             var targetDirection = Vector3.RotateTowards(current: player.transform.forward, target: movementDirection, maxRadiansDelta: rotationSpeed * Time.deltaTime, maxMagnitudeDelta: 0.0f);
    55.             character.transform.rotation = Quaternion.LookRotation(targetDirection);
    56.  
    57.             // Background and airplane direction action!
    58.             if(character)
    59.             {
    60.                 moveX = joystick.Horizontal;
    61.                 moveZ = joystick.Vertical;
    62.  
    63.                 movementDirection = new Vector3(moveX * resultSpeed, 0, moveZ * resultSpeed);
    64.                 movement = inputs.movementDirection.normalized;
    65.                 player.transform.Translate(movement);
    66.                 //player.transform.Translate(movementDirection);
    67.             }    
    68.         }
    69.     }
    70. }

    Here part...

    movementDirection = new Vector3(moveX * resultSpeed, 0, moveZ * resultSpeed);
    movement = inputs.movementDirection.normalized;
    player.transform.Translate(movement);
    //player.transform.Translate(movementDirection);

    ...I tried to apply this part, but the player just stops and rotates.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,371
    Don't run the direction-change code if the magnitude of the input is below a certain amount.

    You probably can fix line 48 to just check if it is less than 0.1f instead of 0