Search Unity

Other How to make the player move in the direction indicated by the joystick - isometric camera

Discussion in 'Scripting' started by einnerlink, Nov 29, 2022.

  1. einnerlink

    einnerlink

    Joined:
    Mar 17, 2022
    Posts:
    3
    Hello everyone! I am creating a 3d android game with style similar to Tunic: camera with isometric view, joystick, and so on; However, since I'm not very good at programming, I don't know how to adjust the player's movement with the corresponding direction on the joystick. I mean, if the controller is moved up the character should go in that direction, but obviously, due to the fact that the camera is rotated, an adjustment or trick must be made to compensate for this situation that occurs with this type of mechanic.




    SO, I've seen several featured tutorials but I don't know how to fit it into what I already have built. And this is what I got at the moment:

    My code to move the player:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityStandardAssets.CrossPlatformInput;
    5. using UnityEngine.UI;
    6. using Cinemachine;
    7.  
    8. public class PlayerMove : MonoBehaviour
    9. {
    10.     public Joystick MoveJoystick;
    11.     public float moveSpeed;
    12.     float H, V;
    13.     Rigidbody rig;
    14.  
    15.     void Start()
    16.     {
    17.         rig = GetComponent<Rigidbody>();
    18.         CVM = GameObject.Find("CamaraPrincipal").GetComponent<CinemachineVirtualCamera>();
    19.     }
    20.     float angleA;
    21.  
    22.     void Update()
    23.     {
    24.         if (MoveJoystick.inputVector.x != 0 && MoveJoystick.inputVector.y != 0)
    25.         {
    26.             transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    27.         }
    28.         if (Mathf.Atan2(MoveJoystick.inputVector.x, MoveJoystick.inputVector.y) * Mathf.Rad2Deg != 0)
    29.         {
    30.             angleA = Mathf.Atan2(MoveJoystick.inputVector.x, MoveJoystick.inputVector.y) * Mathf.Rad2Deg;
    31.         }
    32.         transform.eulerAngles = new Vector3(0f, angleA, 0);
    33.     }
    34. }
    And this is the code for my joystick:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class Joystick : Joystick
    5. {
    6.     [Header("Fixed Joystick")]
    7.  
    8.     Vector2 joystickPosition = Vector2.zero;
    9.     private Camera cam = new Camera();
    10.     public AudioSource pasos;
    11.     private bool pasosActivo;
    12.  
    13.     void Start()
    14.     {
    15.         joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
    16.     }
    17.  
    18.     public override void OnDrag(PointerEventData eventData)
    19.     {
    20.         Vector2 direction = eventData.position - joystickPosition;
    21.         inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
    22.         handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
    23.     }
    24.  
    25.     public override void OnPointerDown(PointerEventData eventData)
    26.     {
    27.         OnDrag(eventData);
    28.     }
    29.  
    30.     public override void OnPointerUp(PointerEventData eventData)
    31.     {
    32.         inputVector = Vector2.zero;
    33.         handle.anchoredPosition = Vector2.zero;
    34.     }
    35. }