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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

unity 2017 3d rpg q&a script

Discussion in 'Scripting' started by yuninsik, Jun 27, 2020.

  1. yuninsik

    yuninsik

    Joined:
    Jul 22, 2018
    Posts:
    6


    The left is better at controlling the player character to the controller, but when you press the B button to the right, the character attacks.

    This should be when you built the apk android phone. When the B button is pressed on the pc as well, the character attacks.

    And if you don't attack when you click with a normal mouse without the B button, how do you fix it? Only the phone should have the joystick feature.

    Input.GetButtonDown (FireAxis)

    Is there any other way without writing the above function?

    Working on Unity 2017.

    Below is the source for the joystick script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Events;
    6. using UnityEngine.EventSystems;
    7. using LeoLuz.PropertyAttributes;
    8. #if UNITY_EDITOR
    9. using UnityEditor;
    10. #endif
    11.  
    12. namespace LeoLuz.PlugAndPlayJoystick
    13. {
    14.     [RequireComponent(typeof(Rect))]
    15.     public class UIButtonToButton : ButtonBase, IPointerDownHandler, IPointerUpHandler
    16.     {
    17.         [InputAxesListDropdown]
    18.         public string ButtonName;
    19.         [ReadOnlyInPlayMode]
    20.         public bool pressed;
    21.  
    22.         #if UNITY_EDITOR
    23.         private bool OrderOfScriptChanged;
    24.  
    25.         public void OnDrawGizmosSelected()
    26.         {
    27.             if (!OrderOfScriptChanged)
    28.             {
    29.                 // Get the name of the script we want to change it's execution order
    30.                 string scriptName = typeof(UIButtonToButton).Name;
    31.  
    32.                 // Iterate through all scripts (Might be a better way to do this?)
    33.                 foreach (MonoScript monoScript in MonoImporter.GetAllRuntimeMonoScripts())
    34.                 {
    35.                     // If found our script
    36.                     if (monoScript.name == scriptName && MonoImporter.GetExecutionOrder(monoScript) != -2000)
    37.                     {
    38.                         MonoImporter.SetExecutionOrder(monoScript, -2000);
    39.                     }
    40.                 }
    41.                 OrderOfScriptChanged = true;
    42.             }
    43.         }
    44.         #endif
    45.         public enum CharacterState
    46.         {
    47.             IDLE = 0,
    48.             WALK = 1,
    49.             ATTACK = 2,
    50.             SKILL = 3,
    51.             SIZE
    52.         }
    53.         private CharacterState state = CharacterState.IDLE;
    54.         private int _touchInt;
    55.         private BoxCollider _collider;
    56.         public GameObject Target;
    57.         public string FunctionName;
    58.  
    59.         public override void Start()
    60.         {
    61.             base.Start();
    62.             //bool check;
    63.         }
    64.         public void Update()
    65.         {
    66.            // if (check)
    67.              //   state = CharacterState.ATTACK;
    68.             if (pressed)
    69.                 Input.PressButtonMobile(ButtonName);
    70.            
    71.             if (Input.GetTouch(_touchInt).phase == TouchPhase.Began)
    72.             {
    73.                 RaycastHit hit;
    74.                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(_touchInt).position);
    75.                 if (Physics.Raycast(ray, out hit))
    76.                 {
    77.                     if (hit.collider == _collider)
    78.                     {
    79.                         //if (_buttonScaleUse) _buttonScale = true;
    80.                         //if (Target != null)
    81.                         //{
    82.                         //    Target.SendMessage(FunctionName, SendMessageOptions.DontRequireReceiver);
    83.                         //}
    84.                         state = CharacterState.ATTACK;
    85.                     }
    86.                 }
    87.             }
    88.         }
    89.  
    90.         public void FixedUpdate()
    91.         {
    92.             if (pressed)
    93.                 Input.PressButtonMobile(ButtonName);
    94.             //if (state == CharacterState.ATTACK || state == CharacterState.SKILL)
    95.             //{
    96.             //    return;
    97.             //}
    98.             //if (Input.GetMouseButtonDown(0))
    99.             //{
    100.             //       state = CharacterState.ATTACK;
    101.             //}
    102.         }
    103.  
    104.         void LateUpdate()
    105.         {
    106.      
    107.         }
    108.  
    109.         public virtual void OnPointerDown(PointerEventData data)
    110.         {
    111.             pressed = true;
    112.             Input.PressButtonDownMobile(ButtonName);
    113.             state = CharacterState.ATTACK;
    114.         }
    115.  
    116.         public virtual void OnPointerUp(PointerEventData data)
    117.         {
    118.             pressed = false;
    119.             Input.PressButtonUpMobile(ButtonName);
    120.         }
    121.     }
    122. }
    It is the source of the B button.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using LeoLuz.PropertyAttributes;
    5.  
    6. namespace LeoLuz.PlugAndPlayJoystick
    7. {
    8.     public class SimpleController : MonoBehaviour
    9.     {
    10.         public string HorizontalAxis = "Horizontal";
    11.         public string JumpAxis = "Jump";
    12.         public string FireAxis = "Fire 1";
    13.         public GameObject Projectile;
    14.       //  Rigidbody rb;
    15.         public float velocity = 5f;
    16.         public float ProjectileVelocity = 7f;
    17.         public bool grounded;
    18.         // Use this for initialization
    19.         void Start()
    20.         {
    21.             //rb = GetComponent<Rigidbody>();
    22.         }
    23.  
    24.         //void OnGUI()
    25.         //{
    26.         //    GUILayout.BeginVertical();
    27.         //    GUILayout.Label("Input.GetButtonDown(FireAxis)=" + Input.GetButtonDown(FireAxis));
    28.         //    GUILayout.Label(Input.GetButtonDownList());
    29.         //    GUILayout.EndVertical();
    30.         //}
    31.         //public enum CharacterState
    32.         //{
    33.         //    IDLE = 0,
    34.         //    WALK = 1,
    35.         //    ATTACK = 2,
    36.         //    SKILL = 3,
    37.         //    SIZE
    38.         //}
    39.         //private CharacterState state = CharacterState.IDLE;
    40.         // Update is called once per frame
    41.         void Update()
    42.         {
    43.            // rb.velocity = new Vector3(Input.GetAxis("Horizontal") * velocity, rb.velocity.y);
    44.  
    45.             if (grounded && Input.GetButton(JumpAxis))
    46.             {
    47.              //   rb.velocity = new Vector3(rb.velocity.x, 10f);
    48.                 grounded = false;
    49.             }
    50.  
    51.             if (Input.GetButtonDown(FireAxis))
    52.             {
    53.                 GameObject Player = (GameObject)Instantiate(Projectile, transform.position + Vector3.right * 0.3f, Quaternion.identity);
    54.                 Physics2D.IgnoreCollision(Player.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    55.                // Player.GetComponent<Rigidbody>().velocity = new Vector3(ProjectileVelocity, 0f);
    56.                 //state = CharacterState.ATTACK;
    57.             }
    58.         }
    59.  
    60.         void OnCollisionEnter2D(Collision2D col)
    61.         {
    62.             grounded = true;
    63.         }
    64.  
    65.         void OnCollisionStay2D(Collision2D col)
    66.         {
    67.             grounded = true;
    68.         }
    69.  
    70.         void OnCollisionExit2D(Collision2D col)
    71.         {
    72.             grounded = false;
    73.         }
    74.     }
    75. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    There are predefines such as this:

    https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    You can use those to selectively disable (or enable) parts you don't want in certain targets. These are compile time decisions.

    There are other decisions you can make by introspecting members of the
    Application
    class at runtime.
     
  3. yuninsik

    yuninsik

    Joined:
    Jul 22, 2018
    Posts:
    6
    Thank you for your answer, but should I work on the 2019.4 version, not UNITY 2017?
    I fixed #if unity_editor to #if unity_android, but it hasn't changed.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Nobody can answer that for you. You have to evaluate the two versions against your needs and your project's ability to be easily updated. NOTE: once you upgrade a project you can't go back, so make sure you use source control or make back ups.