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

3D rpg script Q&A

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

  1. yuninsik

    yuninsik

    Joined:
    Jul 22, 2018
    Posts:
    6
    Hello, I am a student who is developing 3D rpg as UNITY in Korea.


    I want to use the free joystick set of Unity 2017 on the Unity Asset Store site.


    It's good for the character to move, but if you press the B button, you have to attack, but I'm asking you because it's not working well.


    I refer to a book called Unity Game Programming written in Korea, and the terrain was made by using a free set.


    The book said UNITY 4.5.


    I think it's a script problem. How can I fix it?


    I'm sending you a screenshot attached.


    I added the joystick to build it as an apk so it can be played on the phone.

     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    Hello @yuninsik ,
    Could you elaborate a bit more what is not working well? And what kind of scripting issue you are experiencing?
     
    yuninsik likes this.
  3. yuninsik

    yuninsik

    Joined:
    Jul 22, 2018
    Posts:
    6
    Is the attached screenshot clear?

    If you look at the screenshot, there's B button on the right. When I press the B button (touch on mobile, I assume), the player character should attack, but it's not. That's why I'm inquiring.


    This is the script for the palyer character.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using LeoLuz.PropertyAttributes;
    namespace LeoLuz.PlugAndPlayJoystick
    {
    public class SimpleController : MonoBehaviour
    {
    public string HorizontalAxis = "Horizontal";
    public string JumpAxis = "Jump";
    public string FireAxis = "Fire 1";
    public GameObject Projectile;
    // Rigidbody rb;
    public float velocity = 5f;
    public float ProjectileVelocity = 7f;
    public bool grounded;
    // Use this for initialization
    void Start()
    {
    //rb = GetComponent<Rigidbody>();
    }
    //void OnGUI()
    //{
    // GUILayout.BeginVertical();
    // GUILayout.Label("Input.GetButtonDown(FireAxis)=" + Input.GetButtonDown(FireAxis));
    // GUILayout.Label(Input.GetButtonDownList());
    // GUILayout.EndVertical();
    //}
    public enum CharacterState
    {
    IDLE = 0,
    WALK = 1,
    ATTACK = 2,
    SKILL = 3,
    SIZE
    }
    private CharacterState state = CharacterState.IDLE;
    // Update is called once per frame
    void Update()
    {
    // rb.velocity = new Vector3(Input.GetAxis("Horizontal") * velocity, rb.velocity.y);
    if (grounded && Input.GetButton(JumpAxis))
    {
    // rb.velocity = new Vector3(rb.velocity.x, 10f);
    grounded = false;
    }
    if (Input.GetButtonDown(FireAxis))
    {
    GameObject Player = (GameObject)Instantiate(Projectile, transform.position + Vector3.right * 0.3f, Quaternion.identity);
    Physics2D.IgnoreCollision(Player.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    // Player.GetComponent<Rigidbody>().velocity = new Vector3(ProjectileVelocity, 0f);
    state = CharacterState.ATTACK;
    }
    }
    void OnCollisionEnter2D(Collision2D col)
    {
    grounded = true;
    }
    void OnCollisionStay2D(Collision2D col)
    {
    grounded = true;
    }
    void OnCollisionExit2D(Collision2D col)
    {
    grounded = false;
    }
    }
    }
     
  4. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    Thank you for sharing the code. (For future reference, do use the "Insert Code" feature when posting blocks of code, this will make the code a lot more readable).

    From looking at the code, I am not sure how you are connecting the B button on the UI to the action of firing. The only thing you have there is
    Input.GetButtonDown(FireAxis)
    . FireAxis is a variable that contains Fire 1, which if left to the default value corresponds to space bar and left mouse button.

    If you are unsure about how to work with the UI inside Unity, I would advice you to have a look at one of the resources online.

    (Brackeys YouTube videos are very good for instance)


    Once you can build a basic UI and connect your MonoBehaviours with UI button interactions, it will be a lot easier to tackle the task you are currently trying to solve.

    Best of luck, and let me know how it goes!