playMaker

Author Topic: [nGUI/Physics] 2D Charactermovement via GUI-Buttons  (Read 4470 times)

Mayhem

  • Full Member
  • ***
  • Posts: 171
[nGUI/Physics] 2D Charactermovement via GUI-Buttons
« on: January 05, 2013, 09:37:17 AM »
Hey there!

I recently purchased this great product and i really really like it. I am still studying some of the tutorials and trying to apply some of the tutorials to my personal needs.

So i have a certain question respectively a problem. I searched this forum and didn't find a solution (maybe i am too dumb to search properly, so if there IS actually a solution: I am very sorry, would be really grateful if someone just could give a link to the topic!)

First of all, here are some details, so you guys can structure the Project:

Genre: a 2D Jump'n'Run Game for the iOS
Plugins: Uni2D (2D Framework for the Leveldesign and some Spriteanimations), SmoothMoves (2D BoneAnimations),  nGUI (for the work with GUI-Elements), FarseerUnity (Physics), of course PlayMaker and some other Plugins which doesn't matter right now to discuss my problem


So i built my level with Uni2D, arranging the Sprites like i want them to; then i gave the Sprite-Plattforms with FarseerUnity Physics like this:



The Playercharacter is a SmoothMoves-BoneAnimation-Gameobject which also has FarseerUnity-Physics in order to move around.

The GUI is very basic, a Life-Gauge, a Pause-Button, three MoveButtons (Left, Right, Jump) and an Attack-Button. As mentioned above, it is made with nGUI. The Movement-Buttons and the Pause-Button are all UIImage-Buttons.


So, the problem is:
I would like to move my character with the buttons via PlayMaker! I can't manage to do that.

I moved my character around with the keyboard with this script:

Code: [Select]
void Update ()
{
if(Input.GetKey(KeyCode.RightArrow))
{

float velocityScale = 2.5f;
velocity.Y = character.LinearVelocity.Y;
velocity.X =  velocityScale * m_normal.X;

character.LinearVelocity = velocity;

}
else if(Input.GetKey(KeyCode.LeftArrow))
{
float velocityScale = 2.5f;
velocity.Y = character.LinearVelocity.Y;

velocity.X =  velocityScale * -m_normal.X;
character.LinearVelocity = velocity;
}
else if(Input.GetKey(KeyCode.UpArrow))
{
float velocityScale = character.Mass * 0.5f;
character.ApplyLinearImpulse(new FVector2(0, velocityScale));
}
}

THEN i found here in the forum this link: http://www.tasharen.com/?page_id=160
and tried things out. I had to made three different Movement-Scripts which I attached to the UIImage-Button-Objects, and all had the OnClick()-Function, like for example the Jump-Script:

Code: [Select]
public GameObject characterObject;

// Farseer Bodies
private Body character;


//Farseer Vectors
private FVector2 velocity;
private FVector2 m_normal;


// Use this for initialization
void Start ()
{

character = characterObject.GetComponent<FSBodyComponent>().PhysicsBody;

character.FixedRotation = true;

velocity = character.LinearVelocity;
m_normal = new FVector2(1,0);

}


void OnClick()
{
float velocityScale = KnightShape.Mass * 5.5f;
character.ApplyLinearImpulse(new FVector2(0, velocityScale));
}

It did work, but i don't want to use it like that at all.

I would prefer an elegant solution with PlayMaker. Can someone help? :)
« Last Edit: January 05, 2013, 03:44:32 PM by Mayhem »

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
Re: [nGUI/Physics] 2D Charactermovement via GUI-Buttons
« Reply #1 on: January 05, 2013, 03:40:08 PM »
Hi :)

This is all purely speculation, but what I would try is using the "GUILayout Repeat Button" function to detect if the button is being pressed - let's say this button is "move left"

this would send an event to a listener which would then go into a new state with a Controller Move or Controller Simple Move function which would apply force to my player character so it would move left

not sure if this only works with pressing the button once or holding down for a constant stream of movement data but it's worth a shot

-Koztheboss
Remember, you don't fail unless you give up trying to succeed!

Mayhem

  • Full Member
  • ***
  • Posts: 171
Re: [nGUI/Physics] 2D Charactermovement via GUI-Buttons
« Reply #2 on: January 05, 2013, 03:49:13 PM »
First, thanks for the reply.

Mh, i don't think that will work.
Will all that "GUI"-related stuff really work since i use nGUI, will it not?

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
Re: [nGUI/Physics] 2D Charactermovement via GUI-Buttons
« Reply #3 on: January 06, 2013, 10:16:06 AM »
I'm sorry, I haven't worked with NGui before, so I'm afraid i can't help you with that

edit:

found this on the first page of help forums - I'm pretty sure you can use something like this:

http://hutonggames.com/playmakerforum/index.php?topic=2856.0

if nothing else, then quit the nGUI stuff and just use normal gui texture instead :)
« Last Edit: January 06, 2013, 10:18:58 AM by KozTheBoss »
Remember, you don't fail unless you give up trying to succeed!

Mayhem

  • Full Member
  • ***
  • Posts: 171
Re: [nGUI/Physics] 2D Charactermovement via GUI-Buttons
« Reply #4 on: January 06, 2013, 10:29:22 AM »
Yeah, i guess i won't do that, because nGUI is way to great. But i think i'll do a little workaround, i think, this idea should work:

When a Button is clicked it sends a Message to the Script which is attached to the Character and calls a certain function like "void WalkLeft()"

KozTheBoss

  • Full Member
  • ***
  • Posts: 150
  • You dont fail unless you give up trying to succeed
    • Pixel Life - portfolio
Re: [nGUI/Physics] 2D Charactermovement via GUI-Buttons
« Reply #5 on: January 06, 2013, 11:17:37 AM »
could work - same principle as adding force to the character via playmaker :P
Remember, you don't fail unless you give up trying to succeed!