- Home /
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"); } }
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")).
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!
wanna post your code on how you are calling the Drive() function and from where?
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);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613100515im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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