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 /
avatar image
0
Question by Mypenguinbuddy · Aug 18, 2015 at 11:44 AM · c#onguigui-button

c# GUI button controls

So I'm trying to convert my tile-based game into a mobile game, and I need to change the controls from keyboard keys to GUI buttons. I have my player moving in 4 directions: up, down, left and right. I need each of these directions to have it's own buttons. I've watched a lot a of videos and look at lots of other answers, but I can't figure out how to do it. I know that I need to use the OnGUI function, and I've tried, but it says that I cant use "&&."

Here is my current player script for the keyboard, but how would I integrate the OnGUI function into this to have touch controls?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour 
 
 {
     
     Vector3 pos;                                // For movement
     float speed = 2.0f;                           // Speed of movement
     static bool[] moving;
     
     void Start () {
         moving = new bool[5];
         pos = transform.position;          // Take the initial position
         GetComponent<BoxCollider2D>().enabled = false;
     }
     
     void FixedUpdate () {
         if (gameObject.tag == "Player") {
             moving[4] = !(Vector2.Distance(transform.position, pos) < 0.01f);
         }
         else {
             moving[GetComponent<CollectFruit>().ID] = !(Vector2.Distance(transform.position, pos) < 0.01f);
         }
         bool doneMoving = DoneMoving ();
         if (doneMoving) {
             GetComponent<BoxCollider2D>().enabled = true;
         }
         if(Input.GetKey(KeyCode.A) && doneMoving && transform.position.x > -1.9f) {        // Left
             pos += Vector3.left;
         }
         if(Input.GetKey(KeyCode.D) && doneMoving && transform.position.x < 1.9f) {        // Right
             pos += Vector3.right;
         }
         if(Input.GetKey(KeyCode.W) && doneMoving && transform.position.y < 1.9f) {        // Up
             pos += Vector3.up;
         }
         if(Input.GetKey(KeyCode.S) && doneMoving && transform.position.y > -1.9f) {        // Down
             pos += Vector3.down;
         }
         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);    // Move there
     }
     public bool DoneMoving() {
         return !moving[0] && !moving[1] && !moving[2] && !moving[3] && !moving[4];
     }
 }
Comment
Add comment · Show 1
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 NeverHopeless · Aug 18, 2015 at 11:58 AM 0
Share

Where do you get this error ? What have you tried in OnGUI(), can you please attach it as well. Also, have you looked at new GUI features (https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui) other than OnGUI implementation ?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Baste · Aug 18, 2015 at 12:25 PM

Learn section on the UI tools.

Comment
Add comment · 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
0

Answer by abraham5361 · Aug 18, 2015 at 04:45 PM

Is your problem texture is not appearing? If so then follow me. Add the following codes Note: it does not anything currently then viewing the texture

 //put this in Public class
 public Texture Buttonup;
 public Texture Buttondown;
 public Texture Buttonright;
 public Texture Buttonleft;
 //you cannot change the variable as much as you want as long you change it in GUI.drawtexture
 
 void onGUI () {
    GUI.DrawTexture(new Rect( 5, 5, 50, 50), Buttonup, ScaleMode, ScaleToFit, 1.0F);
 
    GUI.DrawTexture(new Rect(15, 15, 50, 50), Buttondown, ScaleMode, ScaleToFit, 1.0F);
 
    GUI.DrawTexture(new Rect(15, 15, 50, 50), Buttonleft, ScaleMode, ScaleToFit, 1.0F);
 
    GUI.DrawTexture(new Rect(25, 25, 50, 50), Buttonright, ScaleMode, ScaleToFit, 1.0F);
 //Please read under the code to know how to use the code
 }
 
 
 
 


First to change size of textures in new Rect(10, 10, SIZE1, SIZE2) To change position of texture new Rect(POSTION1, POSTION2, 50, 50) After new Rect(1, 1, 1, 1) (here comes name of texture change if you change the variables on top of OnGUI

To add a texture first make a picture using Photoshop or other image editing programs or go search ones in google images. Put it inside your game files and go to unity editor,click on your script and in the inspector select your Images by clicking each one

If you have any other questions ask me

And your welcome

Comment
Add comment · Show 1 · 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 Mypenguinbuddy · Aug 18, 2015 at 04:50 PM 0
Share

The problem is that I can't get the player to move anymore using pos += Vector3.direction, I end up having to use transform.Translate, and that ends up being really glitchy.

avatar image
0

Answer by Positive7 · Aug 18, 2015 at 02:04 PM

Somrthing like this and position GUIButtons for your needs :

 void OnGUI(){
                 if (gameObject.tag == "Player") {
                     moving[4] = !(Vector2.Distance(transform.position, pos) < 0.01f);
                 }
                 else {
                     moving[GetComponent<CollectFruit>().ID] = !(Vector2.Distance(transform.position, pos) < 0.01f);
                 }
                 bool doneMoving = DoneMoving ();
                 if(GUILayout.Button("←") && doneMoving && transform.position.x > -1.9f) {        // Left
                     pos += Vector3.left;
                 }
                 if(GUILayout.Button("→") && doneMoving && transform.position.x < 1.9f) {        // Right
                     pos += Vector3.right;
                 }
                 if(GUILayout.Button("↑") && doneMoving && transform.position.y < 1.9f) {        // Up
                     pos += Vector3.up;
                 }
                 if(GUILayout.Button("↓") && doneMoving && transform.position.y > -1.9f) {        // Down
                     pos += Vector3.down;
                 }
                 transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);    // Move there
             }
     
 
Comment
Add comment · Show 8 · 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 Mypenguinbuddy · Aug 18, 2015 at 05:32 PM 0
Share

Ok, I'm not sure how I'm going to explain this. In my game, I have the player, and in each direction around him, I have 4 other AI. What is happening is only one AI will move, and then that's it. Otherwise its working :).

avatar image Positive7 · Aug 18, 2015 at 05:42 PM 0
Share

Can you post a screenshot, please?

avatar image Mypenguinbuddy · Aug 18, 2015 at 05:44 PM 0
Share

Also, how would I position the buttons if it's GUI.Layout?

avatar image Positive7 · Aug 18, 2015 at 05:53 PM 0
Share

http://docs.unity3d.com/ScriptReference/GUILayout.BeginArea.html

avatar image Mypenguinbuddy · Aug 18, 2015 at 06:10 PM 0
Share

alt text

All 5 characters are supposed to move as one, but when I clicked the down button, only slime4 moved.

fp-screenshot.png (133.5 kB)
Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[HELP] C# GUI, not showing up in camera 0 Answers

How to hide/show GUI Buttons through another GUI Button? 2 Answers

Display Gui Button On 3d Object location 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