• 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 daclu_10 · Jul 02, 2014 at 07:27 AM · javascriptgui-button

controlling rigidbody 2d

Hi,

I've tried a lot of ways without success, please help me!

I need to move and object that is falling from the top of the screen due to gravity Scale (it has a rigidbody2D and a boxcollider2d components attached).

I've created two GUI.Button , a left and a right arrow.

and the idea its very simple, when the user taps one of those , the object has to move -1 or 1 (depending on which one the user taps) in the x axis, so i can chose where the object will land.

as i said i've tried a lot! still don't know what am i doing wrong. I succeeded when trying with an object that hasn't any component attached, using :

myobject.gameObject.transform.Translate (-1,0,0); (for the left arrow)

but with it comes to objects affected by physics it does not work, but it compiles! so i've got no idea what do i have to change!

please help me!!

Comment
Add comment · Show 1
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 daclu_10 · Jul 02, 2014 at 02:58 AM 0
Share

thanks, my code is this one:

function OnGUI () {

  if (GUI.Button (new Rect (10,10,60,30), "boton")) {
  
 
  
  var obj_to_move = falling_objs.chosen_obj.gameObject; 
  
  Debug.Log (obj_to_move.gameObject.name);
  
  obj_to_move.gameObject.transform.position.x = falling_objs.chosen_pos;
  
  Debug.Log (obj_to_move.gameObject.transform.position.x);
  
  obj_to_move.gameObject.transform.Translate (-1,0,0) ;
  
   
  
  }
  

}

please note that im using 3 variables, obj_to_move that its the gameObject that is falling due to gravity, and is the one i need to move.

the other 2 variables are static, defined in other script called falling_objs

chosen_obj its the object picked randomply from an array.

and chosen_pos its the position.x of this chosen_obj


i get the name of the chosen_obj each time i press the button, and also the obj_to_move.position.x that its = to chosen_pos.

this tell me that there is no problem locating the object that i want to move, BUT IT DOES NOT $$anonymous$$OVE AT ALL!

i've tried a lot, here i post this script cause it worked with an object that hasn't components attached, is the closest result that i've got.

Please help me, i've been trying for a looong time now.

BerggreenD$$anonymous$$, how can i get paid support then?

I'm not asking anyone to work, just to tell me what am i doing wrong, or which rode to take so i can read about it and then try to apply it.

thanks.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How do I single out one object amongst a group of objects with the same tag? 3 Answers

Calling A Function When A Variable Changes? Help 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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