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. Dismiss Notice

Move to direction (Android Joystick)

Discussion in 'Scripting' started by TanmayTNT87, Jun 16, 2019.

  1. TanmayTNT87

    TanmayTNT87

    Joined:
    Jun 3, 2019
    Posts:
    5
    Hello guys i am trying to make 3rd person controller for my android game.... I want to make my character start running from idling in the direction of joystick is dragged. Please help me with this isse. I have very little knowledge of c#.

    I want to make my controller like this game.
     

    Attached Files:

  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    A good remedy for this is a book.
     
  3. TanmayTNT87

    TanmayTNT87

    Joined:
    Jun 3, 2019
    Posts:
    5
    Yes sir i am trying to learn c# i am not totally noob and practicing coding as well. But can you help me with my current problem. I am able to move in direction of my camera facing but i want to bind it with my joystick and i dont know how to do that atm.
    Thank you for your recommendations tho ☺.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
  5. TanmayTNT87

    TanmayTNT87

    Joined:
    Jun 3, 2019
    Posts:
    5
    I am sorry sir, I forgot to explain my problem in details....

    Here is my character controller code with key input (W,A,D) with this code i am able to move my character in direction he is facing (Main Camera is child of my Model so he moves with camera) but i want to control him with joystick handle on mobile i followed a tutorial on YT but then he moves with camera.

    My code -

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class KnightController : MonoBehaviour
    4. {
    5.     float speed = 4;
    6.     float rotSpeed = 80;
    7.     float gravity = 8;
    8.     float rot = 0f;
    9.  
    10.     Vector3 moveDir = Vector3.zero;
    11.  
    12.     CharacterController controller;
    13.     Animator anim;
    14.  
    15.     void Start()
    16.     {
    17.         controller = GetComponent<CharacterController>();
    18.         anim = GetComponent<Animator>();
    19.  
    20.     }
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         Movement();
    26.         Attacking();
    27.  
    28.     }
    29.  
    30.     void Movement()
    31.     {
    32.  
    33.         if (controller)
    34.         {
    35.             if (Input.GetKey(KeyCode.W))
    36.             {
    37.  
    38.                 anim.SetInteger("condition", 1);
    39.  
    40.                 moveDir = new Vector3(0, 0, 1);
    41.                 moveDir *= speed;
    42.                 moveDir = transform.TransformDirection(moveDir);
    43.             }
    44.             if (Input.GetKeyUp(KeyCode.W))
    45.             {
    46.              
    47.                 anim.SetInteger("condition", 0);
    48.  
    49.                 moveDir = Vector3.zero;
    50.             }
    51.          
    52.         }
    53.         rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
    54.         transform.eulerAngles = new Vector3(0, rot, 0);
    55.  
    56.         moveDir.y -= gravity * Time.deltaTime;
    57.         controller.Move(moveDir * Time.deltaTime);
    58.     }
    59.  
    60.     void Attacking()
    61.     {
    62.         if (Input.GetKeyDown(KeyCode.S))
    63.         {
    64.  
    65.             moveDir = Vector3.zero;
    66.             anim.SetInteger("condition", 2);
    67.  
    68.         }
    69.      
    70.         if (Input.GetKeyUp(KeyCode.S))
    71.         {
    72.  
    73.             anim.SetInteger("condition", 0);
    74.  
    75.         }
    76.     }
    77.  
    78. }
     
    Last edited: Jun 16, 2019
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536