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 LANDO · Jan 21, 2011 at 11:02 PM · guicharactercontrollermouseclickgui-button

GUI based character control

Sorry if this is vague, my question is how to create a GUI which controls my character from mouse events on the GUI rather than Input.GetKeyDown.

I have a custom GUI built and need to have it control character movement. Currently I'm using the following to trigger animation and movement:

// All animation that are only supposed loop

animation["drive"].wrapMode = WrapMode.Loop;

function Update () { if (!animation.IsPlaying ("startUp") && !animation.IsPlaying ("armDrillAnimation04")) { if (Input.GetKeyDown ("up")) animation.Blend ("drive", 0.3); } }

For my GUI I'm using:

var style : GUIStyle;

var dPadOutline : Texture2D; var dPadUp : Texture2D;

function OnGUI() {

GUI.Button(Rect(790,460,190,190), dPadOutline, style); if(GUI.Button( Rect(794,540,43,43), dPadLeft, "label")) { //subtracted 75 to Ypos Debug.Log("Clicked the d-pad Left"); } }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Steffen Franz · Jan 21, 2011 at 11:30 PM

wrap your animation blending into it's own function inside the first script (only change the animation if it's not already playing), then call this function from inside if(GUI.Button( Rect(794,540,43,43), dPadLeft, "label")).

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 LANDO · Jan 24, 2011 at 05:00 PM 0
Share

I'm a bit confused, I have made the blending it's own function outside of Update

public function Drive () { animation.Blend ("drive", 0.3);}

However when trying to call it from the GUI script I'm not getting a response. I'll keep playing with it to see if there is something I'm overlooking.

Thanks for your response!

avatar image Steffen Franz · Jan 25, 2011 at 04:35 PM 0
Share

wanna post your code on how you are calling the Drive() function and from where?

avatar image
0

Answer by LANDO · Jan 25, 2011 at 05:14 PM

For my Drive() function I'm calling it through an animation script:

//This script control how all the rover animations animations work when specific keys are pressed

 // All animation that are only supposed to play once then hold the final position
 animation["idle"].wrapMode = WrapMode.ClampForever;
 animation["wheelTurnOut"].wrapMode = WrapMode.ClampForever;
 animation["wheelTurnBack"].wrapMode = WrapMode.ClampForever;
 animation["landPose"].wrapMode = WrapMode.ClampForever;
 //animation["startUp"].wrapMode = WrapMode.ClampForever;

 // All animation that are only supposed loop
 animation["drive"].wrapMode = WrapMode.Loop;
 animation["driveInReverse"].wrapMode = WrapMode.Loop;
 animation["rotateRight"].wrapMode = WrapMode.Loop;
 animation["rotateLeft"].wrapMode = WrapMode.Loop;


 public function Drive ()
 {
     if (Input.GetKeyDown ("up"))
     animation.Blend ("drive", 0.3);
 }


 function Update () 
 { 

    if  (!animation.IsPlaying ("startUp") && !animation.IsPlaying ("armDrillAnimation04"))//|| !animation.IsPlaying("rotateLeft"))
         {

     if (Input.GetKeyDown ("up"))    //("-1"))
       animation.Blend ("drive", 0.3);

     if (Input.GetKeyDown ("down"))
        animation.Blend ("driveInReverse", 0.3); 

     else if (Input.GetButtonUp ("Vertical"))
        animation.CrossFade ("StaticPose");


     if (Input.GetButtonDown ("Horizontal"))        // tell to wheels to rotate to the spin position
         animation.CrossFade ("wheelTurnOut", 0.3);

 //tells the rover wheels to rotate right if is is turning right and left if it is rotating left  
    if (Input.GetKeyDown ("right"))    
         animation.Blend ("rotateRight", 0.4); 

    if (Input.GetKeyDown ("left"))    
         animation.Blend ("rotateLeft", 0.4);

    else if (Input.GetButtonUp ("Horizontal"))
        animation.CrossFade ("StaticPose");

 // If no buttons are being pressed then Wheels will return to their default position
    if (Input.GetButtonUp ("Horizontal"))
        animation.CrossFade ("wheelTurnBack", 1);
         }

 }

 function OnTriggerEnter () 

 {   
     animation.Play ("armDrillAnimation04");
 }

The character (Mars Rover in this case) is controlled with "SampleMoveScript.js" which seems to be my main hangup right now, as I have been able to trigger animations with my new GUI script but cannot link character movement with it. Here is my onGUI() function:

function OnGUI() {

// dPadUp is linked to the "drive" animation here: if(GUI.RepeatButton (Rect (870,468,43,43), dPadUp, "label") || (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Alpha1)) { rover.animation.Blend ("drive", 0.3); // this call to move the character is not working:
rover.transform.TransformDirection(Vector3.up); Debug.Log("Available id: " + GUIUtility.GetControlID(FocusType.Passive)); print("DRIVE!!!"); }

 GUI.Button(Rect(790,460,190,190), dPadOutline, style);  
 //(784,460,200,200)
 if(GUI.Button( Rect(794,540,43,43), dPadLeft, "label")) { //subtracted 75 to Ypos
 Debug.Log("Clicked the d-pad Left");
 }

 if(GUI.Button( Rect(932,540,43,43), dPadRight, "label")) {
 Debug.Log("Clicked the d-pad Right");
 }

 if(GUI.Button(Rect(870,468,43,43), dPadUp, "label")) {
 Debug.Log("Clicked the d-pad Up");

// Drive(); }

 if(GUI.Button(Rect(870,609,43,43), dPadDown, "label")) {
 Debug.Log("Clicked the d-pad Down");
 }

 // Our GUI is laid out for a 1024x768 pixel display. The next line makes sure it rescales nicely to other resolutions.
 GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));

 //Draw bottom GUI section (main)
 DrawImageBottomAligned( mainImageOffset, mainImage); // main image. 

/// Detects keys pressed and prints their keycode var e : Event = Event.current;

 if (e.isKey)
 {
     Debug.Log("Detected key code: " + e.keyCode);
 }

}

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

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

No one has followed this question yet.

Related Questions

Assign keystrokes to GUI button 2 Answers

Buttons that remove themselves when clicked mess up other gui elements 2 Answers

Change button texture when its clicked 1 Answer

GUI controlled movement 0 Answers

Why can't i click a button when in first person? 2 Answers


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