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

5 Replies

· Add your reply
  • Sort: 
avatar image
1
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
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
avatar image
0

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
Add comment · 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
0

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
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
avatar image
0

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
Add comment · 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
0

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
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

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


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