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

Question How would I add analog movement support to this script?

Discussion in 'Scripting' started by XYX1993XYX, Jun 15, 2023.

  1. XYX1993XYX

    XYX1993XYX

    Joined:
    Oct 2, 2019
    Posts:
    14
    Wondering how to use analog with this script for unity 2019

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Frog : MonoBehaviour
    7. {
    8.     public Camera[] cameras = new Camera[2];
    9.     public Rigidbody rb;
    10.     public AudioSource audioSource;
    11.     public AudioClip[] audioClips = new AudioClip[2];
    12.     private bool changeClip = true;
    13.     private int toggle = 0;
    14.  
    15.     void Update()
    16.     {
    17.         if(changeClip)
    18.             audioSource.clip = audioClips[0];
    19.  
    20.         if (Input.GetKeyDown(KeyCode.RightArrow))
    21.         {
    22.             StartCoroutine(Jump());
    23.             this.gameObject.transform.eulerAngles = new Vector3(0, 90, 0);
    24.             rb.MovePosition(rb.position + Vector3.right);
    25.             audioSource.Play();
    26.         }
    27.         else if (Input.GetKeyDown(KeyCode.LeftArrow))
    28.         {
    29.             StartCoroutine(Jump());
    30.             this.gameObject.transform.eulerAngles = new Vector3(0, -90, 0);
    31.             rb.MovePosition(rb.position + Vector3.left);
    32.             audioSource.Play();
    33.         }
    34.         else if (Input.GetKeyDown(KeyCode.UpArrow))
    35.         {
    36.             StartCoroutine(Jump());
    37.             this.gameObject.transform.eulerAngles = new Vector3(0, 0, 0);
    38.             rb.MovePosition(rb.position + Vector3.forward);
    39.             audioSource.Play();
    40.         }
    41.         else if (Input.GetKeyDown(KeyCode.DownArrow))
    42.         {
    43.             StartCoroutine(Jump());
    44.             this.gameObject.transform.eulerAngles = new Vector3(0, 180, 0);
    45.             rb.MovePosition(rb.position + Vector3.back);
    46.             audioSource.Play();
    47.         }
    48.        
    49.        
    50.        
    51.     }
    52.  
    53.     public void hit()
    54.     {            
    55.             Score.Lifes--;          
    56.             StartCoroutine(death());      
    57.        
    58.     }
    59.  
    60.  
    61.     IEnumerator death()
    62.     {
    63.         changeClip = false;
    64.         audioSource.clip = audioClips[1];
    65.         audioSource.Play();
    66.         Score.CurrentScore = 0;
    67.         Score.time = Score.rememberTime;
    68.         yield return new WaitForSeconds(.4f);
    69.         if (Score.Lifes <= 0)
    70.         {
    71.             SceneManager.LoadScene("TitleScreen");
    72.         }
    73.         else
    74.         {
    75.             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    76.         }
    77.     }
    78.  
    79.     private IEnumerator Jump()
    80.     {
    81.         int i = 0;
    82.         while(i<6)
    83.         {
    84.             this.gameObject.transform.Translate(0, .05f, 0);
    85.             i++;
    86.             yield return new WaitForFixedUpdate();
    87.         }      
    88.  
    89.         yield return null;
    90.     }
    91.  
    92. }
    93.  
     
  2. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    557
    Instead of using GetKeyDown, you could try to use GetAxis and check if it's equal to 1 or -1 for movement. You'll have to add In a check or 2 so it behaves like GetKeyDown, but it'll allow you to use the built in Input system which will make it easier to incorporate controller buttons and joysticks.
     
  3. XYX1993XYX

    XYX1993XYX

    Joined:
    Oct 2, 2019
    Posts:
    14
    thanks! any way you can write a snippet so i can study it?
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    Kurt-Dekker likes this.