Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 nasby321 · Sep 01, 2013 at 10:47 PM · guibuttonside-scrolling

How to set GUI.Button as controls?

I tried several ways, but I always end up with one error. I wan the GUI buttons to do the same thing as the Keys I have set for movement and attack. I have KeyCode.F for attack I want a GUI.Button to do the same thing. I tried to make a GUI.RepeatButton for my KeyCode.A and D for left and right, but that doesn't work either. Here's what I have:

 using UnityEngine;
 using System.Collections;
 using SpriteFactory;
     
 public class player : MonoBehaviour {
     
     public float speed = 1.0f;
         
     private Transform thisTransform;
     private Sprite sprite;
     private bool flipped;
 
     // Use this for initialization
     void Awake () {
         thisTransform = transform;
         sprite = (Sprite)GetComponent(typeof(Sprite));
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 moveVector = new Vector3(0,0,0);
         bool moving = false;
         
         
         
             
         
         if(Input.GetKey(KeyCode.A)){
             moveVector.x -= 1.0f;
             moving = true;
     }
         
         if(Input.GetKey(KeyCode.D)){
             moveVector.x += 1.0f;
             moving = true;
             
         }
         
         if(Input.GetKey(KeyCode.F)){
             Attack();
         } else {
             if(!IsAttacking()) {
                 if(moving) {
                     Move(moveVector);
                 } else
                     Stand();
             }
         }
     }
     
     private void Stand() {
         sprite.Stop();
     }
     
     private void Move(Vector3 moveVector) {
         FlipSprite(moveVector.x);
         moveVector *= speed * Time.deltaTime;
         thisTransform.position = thisTransform.position + moveVector;
         sprite.Play("walk");
     }
     
     private void FlipSprite(float x) {
         if(x == 0.0f) return;
         
         float xDir = Mathf.Sign(x);
         if(!flipped && xDir < 0.0f) {
             flipped = true;
             sprite.SetFlippedState(true, false);
         } else if(flipped&& xDir > 0.0f) {
             flipped = false;
               sprite.SetFlippedState(false, false);
         }
     }
     
     private void Attack() {
         if(IsAttacking()) return;
         
         sprite.Play("Sword");
     }
     
     private bool IsAttacking() {
         if(sprite.IsAnimationPlaying("Sword")) return false;
         return false;
     
 }
     void OnGUI () {
         Vector3 moveVector = new Vector3(0,0,0);
         bool moving = false;
     
 
         // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
         if(GUI.RepeatButton(new Rect(1000,300,80,40), "Sword")) {
             Attack ();
         } else {
             if(!IsAttacking()) {
                 if(moving) {
                     Move(moveVector);
                 } else
                     Stand();
             }
 
         // Make the second button.
         if(GUI.RepeatButton(new Rect(20,500,80,40), "Left")) {
                 moveVector.x -= 1.0f;
                 moving = true;
                 
         
         }
             if(GUI.RepeatButton(new Rect(1000,500,80,40), "Right")) {
                 moveVector.x += 1.0f;
                 moving = true;
                 
         
         }
     }
 }
     
     
 
     
         
     
             
         
     
     
 
 
 }


All it does is places buttons and screws up my waling animation and attacking animation. It only plays first frame of walk animation when I use the a or d key to walk. The buttons don't do anything. Please help me!

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 DevJay · Sep 05, 2013 at 05:18 AM

You shouldn't be using repeat button unless you want the use to have to hold the button down, I suggest using GUI.Button which is for button pressed an button released using single clicks.

Also for your conditional statements you are using a very bad practice such as IsAttacking() of not using brackets. You have

if(foo)return foo;

You must be careful and instead use if (foo) { return foo; }

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 nasby321 · Sep 05, 2013 at 10:40 AM 0
Share

I don't really understand what you mean. I'm still somewhat new to scripting. Yes I want repeat button. If i hold down left button I want him to keep moving left and once released he stops moving.

avatar image nasby321 · Sep 05, 2013 at 12:25 PM 0
Share

Actually I figured out what you meant. It worked I just forgot to add a bracket inside the attack function that messed up the whole script. Thanks a bunch!

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

17 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

Related Questions

Making a Hover function for a button or toolbar from code. 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Question about positioning GUI. 1 Answer

Button reaction time problem 1 Answer

How to change a GUI button texture through scripting 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