Author Topic: Permanent Button-Action  (Read 20038 times)

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Permanent Button-Action
« on: January 10, 2013, 07:21:25 AM »
Hey there.

So i have these UIImageButtons, where i can move my Character through the Level. The Level and the character have some physics (FarseerUnity-Plugin-Physics, static Level, dynamic Character). With the Buttons i am controlling the dynamic Body of the Character.

So, for some testing i also have within an Update()-Method same actions, but only with Keyboard-Events, like this:

  1. if(Input.GetKeyDown(KeyCode.RightArrow))
  2.                 {
  3.                        
  4.                         float velocityScale = 2.5f;
  5.                         velocity.Y = KnightShape.LinearVelocity.Y;
  6.                         velocity.X =  velocityScale * m_normal.X;
  7.                        
  8.                         KnightShape.LinearVelocity = velocity;
  9.                        
  10.                 }

So, my problem is, that via Touch the results aren't the same like on the keyboard. When i press the right arrow and hold it, the character moves all the way to the right till i release my finger from the RightArrow-Key. I tried every meaningful nGUI-Event (OnClick, OnPress etc.), but nothing worked, he just moves with the VelocityScale to the right and stops, so i have to press/click the button all the time to get the character moving.

What am i doing wrong?

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Permanent Button-Action
« Reply #1 on: January 10, 2013, 08:20:14 AM »
You have to deal with OnPress, you must use it bad;

  1. void OnPress (bool isDown)
  2. {
  3.       if(isDown)
  4.       {
  5.             float velocityScale = 2.5f;
  6.             velocity.Y = KnightShape.LinearVelocity.Y;
  7.             velocity.X =  velocityScale * m_normal.X;
  8.            
  9.             KnightShape.LinearVelocity = velocity;
  10.       }
  11. }
Graphicstream Dev.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Permanent Button-Action
« Reply #2 on: January 10, 2013, 08:51:36 AM »
Thanks for the response, but i already tried that, didn't work :/

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Permanent Button-Action
« Reply #3 on: January 10, 2013, 08:55:47 AM »
Did you attach your script to the root of the button? (same level as the BoxCollider)
Graphicstream Dev.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Permanent Button-Action
« Reply #4 on: January 10, 2013, 09:03:06 AM »
Here is the structure:



The selected Button in the Inspector:



Should be the right one, shouldn't it?
The character is moving, but not the way i want him to. Same single-movement, not continuous.


Edit:

When exactly does the bool "isDown" get true/false? Is this done automatically by nGUI?
« Last Edit: January 10, 2013, 09:04:58 AM by BehindTheStone »

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Permanent Button-Action
« Reply #5 on: January 10, 2013, 09:07:53 AM »
Yes that is the event system of NGUI which calls OnPress(true) when the button is pressed.
« Last Edit: January 10, 2013, 09:23:31 AM by Cripple »
Graphicstream Dev.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Permanent Button-Action
« Reply #6 on: January 10, 2013, 09:12:35 AM »
Hmpf, doesn't work :/

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Permanent Button-Action
« Reply #7 on: January 10, 2013, 09:23:45 AM »
Can you copy paste the code of Move Right ?
Graphicstream Dev.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Permanent Button-Action
« Reply #8 on: January 10, 2013, 09:28:07 AM »
Of course :)

  1. public class MoveRight : MonoBehaviour {
  2.  
  3.         private GameObject knightObject;
  4.        
  5.         // Farseer Bodies
  6.         private Body KnightShape;
  7.                
  8.        
  9.         //Farseer Vectors
  10.         private FVector2 velocity;
  11.         private FVector2 m_normal;
  12.        
  13.        
  14.        
  15.        
  16.         ///////////////
  17.         // Functions
  18.         ///////////////
  19.        
  20.  
  21.        
  22.         /// <summary>
  23.         /// Start this instance.
  24.         /// </summary>
  25.         void Start ()
  26.         {
  27.                 knightObject = GameObject.FindGameObjectWithTag("Player");
  28.  
  29.                
  30.                
  31.                 KnightShape = knightObject.GetComponent<FSBodyComponent>().PhysicsBody;
  32.                 KnightShape.FixedRotation = true;
  33.                
  34.                 velocity = KnightShape.LinearVelocity;
  35.                 m_normal = new FVector2(1,0);
  36.                
  37.        
  38.         }
  39.  
  40.        
  41.        
  42.         void OnPress (bool isDown)
  43.         {
  44.                 if(isDown)
  45.                 {
  46.                        
  47.                         float velocityScale = 2.5f;
  48.                         velocity.Y = KnightShape.LinearVelocity.Y;
  49.                         velocity.X =  velocityScale * m_normal.X;
  50.                        
  51.                         KnightShape.LinearVelocity = velocity;
  52.                        
  53.                 }              
  54.         }
  55. }

Thanks for your help!

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Permanent Button-Action
« Reply #9 on: January 10, 2013, 09:36:57 AM »
Just try to add some Debug.Log("Test"); in your function to see if it is called or not. If it is called, that is your physic code which is wrong.
Graphicstream Dev.

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Permanent Button-Action
« Reply #10 on: January 10, 2013, 09:40:31 AM »
Are you sure that you don't change the velocity of your body in another script ?

Also when you have physic bodies I think that you should deal with Forces to make them move.
Graphicstream Dev.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Permanent Button-Action
« Reply #11 on: January 10, 2013, 09:49:46 AM »
So, i made just this:

  1. void OnPress (bool isDown)
  2.         {
  3.                 if(isDown)
  4.                 {
  5.                        
  6.                         float velocityScale = 2.5f;
  7.                         velocity.Y = KnightShape.LinearVelocity.Y;
  8.                         velocity.X =  velocityScale * m_normal.X;
  9.                        
  10.                         KnightShape.LinearVelocity = velocity;
  11.                        
  12.                         Debug.Log("Moved");
  13.                 }              
  14.         }

Hit RUN, and pushed the Button. Every single time he logged me "Moved". So, you're saying the physic code is wrong? Could you kind of point out what exactly could be wrong (i don't expect you to give me the perfect solution  :)  ), i' am just curious, because with the Keyboard-Input it works fine :/

- Yes, i am sure that i am not changing the Velocity with another script. There is only one script right one which influences any velocity, and it's attached to this Button.
- Forces. Yes, i will keep that in mind, thank you!

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Permanent Button-Action
« Reply #12 on: January 10, 2013, 09:57:15 AM »
Everything related to the physic should be executed in the FixedUpdate() anyway.

Try to do it in the FixedUpdate() and use your button press event to change a boolean which informs the fixed update to move the body or not.
« Last Edit: January 10, 2013, 10:01:30 AM by Cripple »
Graphicstream Dev.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: Permanent Button-Action
« Reply #13 on: January 10, 2013, 10:04:35 AM »

Well, i did something like this:

  1. [...]
  2. private bool buttonDown; // which is set to "false" in Start();
  3. [...]
  4.  
  5. void FixedUpdate()
  6.         {
  7.                 if(buttonDown)
  8.                 {
  9.                         float velocityScale = 2.5f;
  10.                         velocity.Y = KnightShape.LinearVelocity.Y;
  11.                         velocity.X =  velocityScale * m_normal.X;
  12.                        
  13.                         KnightShape.LinearVelocity = velocity;
  14.                        
  15.                         Debug.Log("Moved");
  16.                 }
  17.         }
  18.        
  19.        
  20.         void OnPress (bool isDown)
  21.         {
  22.                 if(isDown)
  23.                 {
  24.                         buttonDown = true;     
  25.                 }              
  26.         }

And it worked :)

A little bit of a workaround, though. Another point is, that the developer of this PhysicsPlugin (Farseer-Port) mentioned on their blog that one should avoid FixedUpdate(), when developing for mobile devices (what i am doing right now) to get better performance :D

Cripple

  • TNP Alpha
  • Full Member
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Permanent Button-Action
« Reply #14 on: January 10, 2013, 10:06:13 AM »
It affects only 1 body in the scene and only when you press the button. So it won't affect performances :)
Graphicstream Dev.