• 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
Question by Earth-O-Matic · Mar 21, 2011 at 10:59 PM · guiphysicsbuttonaddforceonmousedown

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!

Comment
EytanTKing

People who like this

1 Show 0
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

5 Replies

  • Sort: 
avatar image
Best Answer

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.

Comment
EytanTKing

People who like this

1 Show 0 · 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

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.

Comment

People who like this

0 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 Earth-O-Matic · Mar 22, 2011 at 03:35 AM 0
Share

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

avatar image

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!

Comment

People who like this

0 Show 0 · 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

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;

Comment

People who like this

0 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 Earth-O-Matic · Mar 23, 2011 at 04:20 AM 0
Share

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.

avatar image

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?

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Check if a button is NOT pressed? 3 Answers

Hide/show GUI Buion 1 Answer

OnMouseOver() OnMouseDown() button raycast help 1 Answer

AddForce forward also adding downward force 0 Answers

GUI Buttons Not Displaying 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