Search Unity

C# script for animation using joystick asset .

Discussion in 'Scripting' started by catafest, Nov 25, 2018.

  1. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    78
    I try to create one C# script for animation using joystick asset https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631
    The link I try to follow like video tutorial is this:

    My project needs to use into 2D animation (without rotation of player).
    The video tutorial comes with a variable named controller the type is CharacterController2D.
    I cannot use https://github.com/Brackeys/2D-Character-Controller.
    Even if it seems a simple problem it is very difficult to implement. Do you have a good video tutorial for last Unity 2018.2.17f1 ( 64-bit ).
    This is the script with variables used like Parameters in Animator and my animations into Base Layer (PlayerIdle, PlayerUp, PlayerLeft... ) :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveByTouch : MonoBehaviour {
    6.     public CharacterController2D controller;
    7.     public Animator animator;
    8.     public Joystick joystick;
    9.     public float runSpeed = 40f;
    10.     float horizontalMove = 0f;
    11.     bool jump = false;
    12.     bool crouch = false;
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         //horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    17.         //horizontalMove = joystick.Horizontal * runSpeed;
    18.         if (joystick.Horizontal >= .2f)
    19.         {
    20.             horizontalMove = runSpeed;
    21.         }
    22.         else if (joystick.Horizontal <= -.2f)
    23.         {
    24.             horizontalMove = -runSpeed;
    25.         }
    26.         else
    27.         {
    28.             horizontalMove = 0f;
    29.         }
    30.  
    31.         float verticalMove = joystick.Vertical;
    32.  
    33.         animator.SetFloat("Speed",Mathf.Abs(horizontalMove));
    34.  
    35.         if (verticalMove >= .5f ) {
    36.             jump = true;
    37.             animator.SetBool("IsUp", true);
    38.         }
    39.  
    40.         if (verticalMove <= -.5f)
    41.         {
    42.             crouch = true;
    43.         } else {
    44.             crouch = false;
    45.         }
    46.         /* if (Input.touchCount > 0)
    47.         {
    48.             Touch touch = Input.GetTouch(0);
    49.             Vector3 touchPossition = Camera.main.ScreenToWorldPoint(touch.position);
    50.             touchPosition.z = 0;
    51.             transform.position = touchPosition;
    52.  
    53.         } */
    54.         /* for (int i = 0; i < Input.touchCount; i++)
    55.         {
    56.             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
    57.             Debug.DrawLine(Vector3.zero, touchPosition, Color.red);
    58.         } */
    59.     }
    60. }
    61.