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
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 thing 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 catching their inputs with the mouse position like this:

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

That you are.

avatar image Noztradamuz · Jun 05, 2013 at 09:00 PM 0
Share

Actually if you pay close attention to the code you will see that im calling the functions from Get$$anonymous$$ouseButton() and not from Get$$anonymous$$ouseButtonDown(), anyway thanks :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

I Managed to do this 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 something curious, for some reason if I call the functions Subir() and Bajar() from somewhere besides the Input.GetKey() function they do nothing. But fortunately I managed to do this 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.

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

Inputing an Equation 0 Answers

Using var as a placeholder for velocity... why? 2 Answers

Parenting Preventing Player Rotation 0 Answers

Multiple Cars not working 1 Answer

How to create a damped harmonic oscillation? 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