- Home /
Adding Force with a GUI button
Nother question for everyone. I have an object that walks back and forth on the screen. I want to be able to use a "jet pack" by clicking a GUI button that will add force to the object. Here's the code I have. Maybe it's completely wrong but I'm trying to basically find the player then add force to it...
var normalTex : Texture2D; var hoverTex : Texture2D; var Player;
//Define Player function Start (){
Player = GameObject.FindWithTag("Player");
}
function OnMouseEnter () {
guiTexture.texture = hoverTex;
}
function OnMouseExit(){
guiTexture.texture = normalTex;
}
//Apply force for Jet Pack function OnMouseDown(){
Player.rigidbody.AddForce (transform.up*1);
Debug.Log("clicked");
}
The button is being clicked but nothing happens to the player. As always, help is really appreciated! Thank you!
Answer by Earth-O-Matic · Mar 24, 2011 at 04:20 AM
Alright figured it out. The code I just put up was correct. but I was multiplying the Y value. When it's not moving up there is nothing to multiply...duh.... any way. Here is the finished code for any one else looking to have a GUI button manipulate an object.
var Jpack : Texture2D; var position : Rect; var style : GUIStyle; var Player : GameObject;
function OnGUI () { if (GUI.Button (position, Jpack, style))
Player.rigidbody.velocity.y += 10;
print ("you clicked the icon");
}
Where GameObject is the object you are trying to manipulate.
I ended up ditching the GUI Texture and went to adding GUI code on the camera making it much cleaner.
Answer by kennypu · Mar 22, 2011 at 01:12 AM
where are you actually drawing the button? the code for what happens when you click on the button should be there. eg:
if(GUI.Button(/*drawing button stuff here*/))
{
//do something that happens when button is pressed
}
I'm assuming you're just using the GUI.Button incorrectly. you should look at the scripting reference next time.
This code is on the button itself. I created a GameObject > GUI texture then applied this script to it. I'll look into GUI.Button
Answer by Earth-O-Matic · Mar 23, 2011 at 12:17 AM
Probably need to re-phrase. The issues I'm having is that the addforce isn't working. I have used this code to effectively do a translate movement like transform.translate (vector3(0,10,0)). I can get it to jump like I want it to but the problem is it jerks up into the air instead of a nice smooth movement like a jet pack would have. So I am trying addforce instead but it is not working. That's the issues I'm having not that the button does nothing.
I am using a GUI texture and applying this script to it and it works. I've gotten it to do a transform.translate and it returns the debug text. My problem is the addforce not working, not the button itself.
If I'm doing this in a less than efficient way please let me know that too. As always, thank you for help!
Answer by efge · Mar 23, 2011 at 12:24 AM
Forces only affect Rigidbodies. So maybe you have to attach a rigidbody to your object.
But be careful, if you want to add forces attach a rigidbody, if you want to control the object without physics (like you did before) set rigidbody.isKinematic = true;
I'm kinda doing a bit of both. I'm giving it constant movement because I don't want the player controlling the character directly but the character can be shot upwards or fall down. so the character has a rigidbody set to it. I'll toy around with it a bit more. See if I can get closer.
Answer by Earth-O-Matic · Mar 23, 2011 at 06:23 AM
Almost there I think. I have changed the button from a GUI Texture to some GUI code on the main camera itself...
var Jpack : Texture2D; var position : Rect; var style : GUIStyle; var Player : GameObject;
function OnGUI () { if (GUI.Button (position, Jpack, style))
Player.rigidbody.velocity.y *= 20;
print ("you clicked the icon");
}
It is definitely manipulating the player now but it seems to be caught up on the ground and not really moving. I need to be clicking the button a lot while its moving around and maybe at some point it will hit something correctly that it will jump in the air. Any thoughts?
Your answer
Follow this Question
Related Questions
OnMouseOver() OnMouseDown() button raycast help 1 Answer
AddForce forward also adding downward force 0 Answers
Check if a button is NOT pressed? 3 Answers
Hide/show GUI Buion 1 Answer
Move GUI elements. 0 Answers