Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by jung-hyun-jong · Sep 21, 2015 at 09:32 AM · c#move an objectgui buttons

Unity 2d make four directions (Up, Down, Right, Left. like RPG games) GUI buttons. Please help me.

First of all, my English ability just a little. I made character and character movement. (Also made Animation, Animator. Four directions) I made to move the keyboard,

Up - keyboard "W" Down - keyboard "S" Right - keyboard "D" Left - keyboard "A"

made in this format(this is a PlayerControllers Script). Format is created as a keyboard works well. But, How to create a GUI button, I do not know what to add, what to PlayerControllers Script. I found roughly, there is a movie that uses GetAxisRaw and Rigidbody2D... I do not know at all. Here are attached to PlayerControllers Script.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControllers: MonoBehaviour {
     
     public Animator anim;
     public float speed;
 
 
 
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator> ();
     }
 
 
 
 
 
 
     // Update is called once per frame
     void Update () {
 
 
         if (Input.GetKey (KeyCode.D)) 
         {
             transform.Translate(Vector2.right * speed);
         }
         
         if (Input.GetKey (KeyCode.A)) 
         {
             transform.Translate(-Vector2.right * speed);
         }
         if (Input.GetKey (KeyCode.W)) 
         {
             transform.Translate(Vector2.up * speed);
         }
         if (Input.GetKey (KeyCode.S)) 
         {
             transform.Translate(-Vector2.up * speed);
         }
 
 
 
 
 
         
         if (Input.GetKey (KeyCode.A)) {
             anim.SetBool ("Left", true);
             anim.SetBool ("Right", false);
             anim.SetBool ("Up", false);
             anim.SetBool ("Down", false);
         }
         if (Input.GetKey (KeyCode.S)) {
             anim.SetBool ("Down", true);
             anim.SetBool ("Up", false);
             anim.SetBool ("Left", false);
             anim.SetBool ("Right", false);
             
         }
         if (Input.GetKey (KeyCode.D)) {
             anim.SetBool ("Right", true);
             anim.SetBool ("Left", false);
             anim.SetBool ("Up", false);
             anim.SetBool ("Down", false);
         }
         if (Input.GetKey (KeyCode.W)) {
             anim.SetBool ("Up", true);
             anim.SetBool ("Down", false);
             anim.SetBool ("Left", false);
             anim.SetBool ("Right", false);
         }
         
         
         
         if(Input.GetKey (KeyCode.A)){
             anim.SetBool("Player_Left", true);
         }
         else{
             anim.SetBool ("Player_Left", false);
         }
         
         
         
         if(Input.GetKey (KeyCode.S)){
             anim.SetBool("Player_Down", true);
         }
         else{
             anim.SetBool ("Player_Down", false);
         }
         
         
         
         if(Input.GetKey (KeyCode.D)){
             anim.SetBool("Player_Right", true);
         }
         else{
             anim.SetBool ("Player_Right", false);
         }
         
         
         
         if(Input.GetKey (KeyCode.W)){
             anim.SetBool("Player_Up", true);
         }
         else{
             anim.SetBool ("Player_Up", false);
         }
         
         
     }
     
     
 }


I would want to ask. To create a GUI button of the four directions, and I want to move character to four directions (Up, Down, Right, Left), but it doesn't work. I do not know what to do in PlayerControllers Script added. Also attach a Game Scene.

alt text

Please help me.

Thank you.

picture.jpg (11.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xephex · Sep 21, 2015 at 10:58 AM

If you go to Create > UI > Button

You can change the OnClick() section on the right to run any function in your code. For example, if you make a function called ChangeDirectionOfCharacter (or whatever you want to call it), set that function to the OnClick event then make a function something like this:

 function ChangeDirectionOfCharacter(dir : String){
       if(dir=="up"){
             Transform.translate(Vector2.Up * speed);
       }
      //continue other "if statements" for other directions.
 }

You need to change the argument in the editor too. It will look like this (or similar): http://i.imgur.com/dDX9XG0.png

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jung-hyun-jong · Sep 22, 2015 at 09:54 AM 0
Share

Have you Create > UI > Button is not rather the Create > UI > Image? Button is something that seems strange.

avatar image jung-hyun-jong · Sep 25, 2015 at 09:59 AM 0
Share

And What is the dir? direction? What Is fits in dir place? Thanks bit $$anonymous$$ore specifically described. please help me...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Click on object shows menu, but also move it. 0 Answers

How do I move a game object only on the x axis with arrow keys? 1 Answer

Moving moving Object with left/right arrow keys in a circular direction 3 Answers

Why the loop in waypoints in not working when set to true? 0 Answers

How do I make objects move with the moving platform? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges