• 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 Noztradamuz · May 30, 2013 at 01:43 AM · c#rigidbodyinputfunction

How to successfully apply force calling a function from another script

Hi, I have a problem that is making me crazy... I have a 2D platform kind off game, where the user can only move on X and Y axis, well the t$$anonymous$$ng is, Im moving the player with Physx applying forces from a C# Scrip called PlayerMovement, I have two functions called Subir() and Bajar() (means up and down)

     public void Subir()
     {
         if (!Atascado)
         {
             rigidbody.AddRelativeForce((Vector3.up * acelVert * Time.deltaTime) * 1.5f, ForceMode.Impulse);
             print("subiendo a "+GetComponent<CharacterProperties>().Name);
             if (detenido)
             {
                 acelVert = 10;
             }
             if (!detenido)
             {
                 acelVert += 50;
             }
             up = true;
         }
     }
 
     public void Bajar()
     {
         if (!Atascado)
         {
             rigidbody.AddRelativeForce((Vector3.up * -acelVert * Time.deltaTime) * 1.5f, ForceMode.Impulse);
             print("bajando a " + GetComponent<CharacterProperties>().Name);
             if (detenido)
             {
                 acelVert = 10;
             }
             if (!detenido)
             {
                 acelVert += 50;
             }
             down = true;
         }
     }

now from the very PlayerMovement Script im calling those functions just like

             if (Input.GetKey(KeyCode.DownArrow))
             {
                 Bajar();
             }
 
             if (Input.GetKey(KeyCode.UpArrow))
             {
                 Subir();
             }

and it works Great! but from other side I have a C# script called GameBasics from there Im drawning some GUITexture buttons and catc$$anonymous$$ng their inputs with the mouse position like t$$anonymous$$s:

                 if (Input.GetMouseButtonDown(0))
                 {
                     if (turboBtn.HitTest(Input.mousePosition))
                     {                        
                         _pMovement.LanzaTurbo();
                     }
                     if (switchPlayerBtn.HitTest(Input.mousePosition))
                     {
                         SwitchPlayer();
                     }
                 }
                 if (Input.GetMouseButton(0))
                 {
                     if (upArrowBtn.HitTest(Input.mousePosition))
                     {
                         _jugador.GetComponent<PlayerMovement>().Subir();
                     }
                     if (dwnArrowBtn.HitTest(Input.mousePosition))
                     {
                         _jugador.GetComponent<PlayerMovement>().Bajar();
                     }
                 }

Now here's the problem the first to "LanzarTurbo()" and "SwitchPlayer()" are working fine when I click the corresponding buttons on the screen, but when I click on the other two, apparently the Subir() and Bajar() functions are being called but no force is been applied to my character. I know Subir() and Bajar() works cause I printed some output on the console and I can see from both ways, with the Keyboard Arrows and with the GUITexture buttons, but the second ones arent really applying the force to my object, any ideas why t$$anonymous$$s is happening? or any suggestions to solve that? as you can see on the PlayerMovement Subir() and Bajar() functions I have a print() function that prints some output, output that can be seen through both ways, but the rigidbody.AddRelativeForce() is not reflecting any changes on my character with the function being called from other script.

Comment
Add comment · Show 2
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 robertbu · May 30, 2013 at 04:40 AM 1
Share
avatar image Noztradamuz · Jun 05, 2013 at 09:00 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Noztradamuz · May 31, 2013 at 03:36 PM

I Managed to do t$$anonymous$$s by creating a bool variable on the PlayerMovement script and calling the function Subir() and Bajar() if the bool variable is true, and I switch those variables on the GetKey and by making the bool variables public I was able to access them from the GameBasics script and apply the force correctly, there is somet$$anonymous$$ng curious, for some reason if I call the functions Subir() and Bajar() from somewhere besides the Input.GetKey() function they do not$$anonymous$$ng. But fortunately I managed to do t$$anonymous$$s correctly, thanks for the quick answer.

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.

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# RotateAround for 2DRigidBodies 1 Answer

Why is Input.GetAxisRaw() not returning whole numbers when using a joystick? 1 Answer

C# Null Reference Exception in Custom Function 1 Answer

Distribute terrain in zones 3 Answers

how can I rotate object reading from excel data & time steps 3 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