playMaker

Author Topic: How to get an Int (1 or -1) depending on direction ? [SOLVED]  (Read 2500 times)

curb47

  • Sr. Member
  • ****
  • Posts: 256
Hello again,

I hope you're all well.

So, another head-scratching scenario... I'm trying to get a simple 1 or -1, depending on the direction my character (spaceship) is facing, and store the value as an Int variable ('direction').

It's a 2.5D sideways shooter, and the spaceship can move left/right; and it rotates on the Y axis to face the direction it's moving in; so the X axis is facing left or right.

I want to add a simple Boost function when a key is pressed.

I've set a variable 'boost' = 20

When Get Key Down (Boost Button) is pressed, it does a quick Set Velocity X = 'boost'.

However, I need to convert 'boost' = 20 to 'boost' = -20, depending on which direction the ship is facing.

So, I'm kind of wanting something like:

If ship faces left, then 'direction' = 1
If ship faces right, then 'direction' = -1

'speed' (new variable) = 'direction' * 'boost'

Get Key Down (Boost Button) > Set Velocity X = 'speed'

I've tried various combinations of Get Axis, Set Vector3, Transform Direction etc, but no joy.

I appreciate any help.

Thanks.



 
« Last Edit: June 27, 2020, 11:05:37 AM by djaydino »

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #1 on: June 26, 2020, 12:51:36 PM »
Hi,

1) Ints are for countable things, or indexes and suchlike. In this case you should stay with floats, which is what’s used in vectors, physics etcetera.

2) it’s very easy: multiply with -1 to get the boost value in negative. I.e.change states with float compare  and multiply with -1 on the left one.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #2 on: June 26, 2020, 01:15:21 PM »
Hi Thore,
Thanks for your help.

The only problem I'm having is getting a constant + or - value depending on the way the ship is facing.

I'm using Get Axis - Horizontal for another action, and using the Stored variable it creates to drive the boost, however, Get Axis only stores a + or - value when the Left/Right key is pressed. When no key is pressed it sets to Zero.

So, I'm trying to work out what action to apply to get a simple + or - depending on which way the X axis is pointing, regardless if a key is being pressed.

At the moment, if I hold down the Right key and hit boost, it works fine; and if I hold down the Left key and hit boost it also works. However, if the space ship is hovering and facing left, when I hit boost it accelerates backwards.

Thanks again.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #3 on: June 26, 2020, 01:18:16 PM »
Just to clarify, the Boost function is a pure hyper-space acceleration type thing. I have a different set-up for regular move Left/Right, so the float value needs to be an instant 1 or -1, not creep up/down from Zero. Know what I mean?


Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #4 on: June 26, 2020, 01:48:43 PM »
As I wrote above, with float compare. You make two states, or three if you start with 0. Both states are identical, only that one compares input against -0.1, go to left state, and if +0.1 go to right state. Now it’s either one or the other, based on your input. Next, you simply set the value to -1 (left) or 1 (right).

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #5 on: June 26, 2020, 01:53:50 PM »
Great, okay, I'll investigate that and see what I get.

Thanks for your help, I really appreciate it.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #6 on: June 26, 2020, 02:47:21 PM »
Man, I'm still not getting this working. Well, I have it working a bit.

I think I need to explain the set-up:

For my Left and Right movement of the ship I am using AddForce, not transform.position, ie, on my first state I have:

Get Axis - Horizontal. Multiplier - 8. Store - 'X-axis Global' (float variable).
Set Vector3 XYZ. X - 'X-axis Global'. vector3 variable - 'dirGlobal' (vector3 variable).
AddForce. Vector - 'dirGlobal'. X - 'X-axis Global'.
Smooth Look at Direction. Target Direction - 'dirGlobal'

I have a similar set up for Vertical thrust, and use gravity for down.

So, picture this example:

I hold down the Right key and the ship reaches a high velocity traveling Right (obviously), I tap the Left key and the ship rotates (on Y axis) to face Left, but due to inertia continues to travel at high velocity to the Right. I hold down the Left key, and eventually the ship slows down and starts moving to the Left. I keep holding down the Left key and the ship reaches high velocity traveling Left. etc etc etc.

My problem is that I can only seem to extract one usable variable from this, which is the 'X-axis Global' from the initial Get Axis action.

This float variable essentially holds 3 states:

Left Key down: float from 0 to -8 (the -8 comes from the multiplier in the Get Axis action)
Right Key down: float from 0 to 8
No keys down: 0

Using the Float Compare action, I have set Float 1 to 'X-axis Global', and Float 2 I have left as 0.

The boost left/right is working fine when the actual keys are being pressed down, ie returning a value...

Less Than - Boost Left
Greater Than - Boost Right

But when the ship is whizzing along due to inertia, and no keys are pressed down (X-axis Global = 0), the boost isn't firing, obviously.

Also, the Equal parameter in Float Compare doesn't leave me with anywhere to go. At the moment I've just set it to a loop, so it just returns no boost.

I'd really like the ship to be able to boost in the direction it's facing, regardless of any keys being pressed down, but for the life of me I can't figure out an action that returns a constant + or - (no 0 value) based on the direction of the X axis.

It's the Zero that's killing me.


Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #7 on: June 26, 2020, 03:29:25 PM »
Can’t wrap my mind around your example. You may overcomplicate things.

Quote
I'd really like the ship to be able to boost in the direction it's facing, regardless of any keys being pressed down, but for the life of me I can't figure out an action that returns a constant + or - (no 0 value) based on the direction of the X axis.

It's the Zero that's killing me.

What you mean by “constant value”? You can just make a variable and in one state set it to 1 and in the other to -1? Maybe you aren’t aware that you don’t have to fill out every event in float compare. You simply don’t change state if it’s equal zero.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #8 on: June 26, 2020, 03:30:29 PM »
I'm now trying the Get Transform action to see if I can get the rotational value of the Y axis, which should be either 90 or -90, and do a compare float on that...

But I now can't figure out how to get the rotation values on Get Transform!

Jeeeeesh, this simple problem is taking me all day!

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #9 on: June 26, 2020, 03:36:52 PM »
They are called transform position, rotation and scale, so you need get rotation. But your rotation is set by input, I assume, so why would you get rotation, if you can just compare input against 0?

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #10 on: June 26, 2020, 03:43:03 PM »
The problem is I can't set a variable based on the Direction the X axis is facing.

I can get properties based on lots of things, but there's nothing apparent enabling me to simply get a value based on the direction.

I tried Get Rotation, which almost worked, but the stupid thing returned the value of 270 when the actual rotation was -90.

If I could get an absolute value of rotation on Y axis, which reads as -90 for facing left, and 90 for facing right, then I'd be all good because I could easily do a Greater/Less than Zero. But Get Rotation returned 270 for left, which is obviously greater than zero.

I'm stumped man. It's not complicated.


Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #11 on: June 26, 2020, 03:43:33 PM »
Should be very simple.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #12 on: June 26, 2020, 03:47:44 PM »
But your rotation is set by input, I assume, so why would you get rotation, if you can just compare input against 0?

But I don't want to compare against Input, because when no keys are pressed (Input) I get a value of Zero. If the value is Zero I get no boost.

The boost is like a hyperspace effect. You press the button and BOOM! off you go in the direction you're facing.

It's an additional movement feature, on top of the standard move left/right. Know what i mean?

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #13 on: June 26, 2020, 03:52:13 PM »
Thanks for taking the time to post the example.

However, that won't work, because by nature the GetAxis action only returns a value when the key is pressed, so when no key is pressed, input=0.

I need a value based on which direction the ship is facing, either -1 or 1.

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: How to get an Int (1 or -1) depending on direction ?
« Reply #14 on: June 26, 2020, 03:54:20 PM »
It's really unclear where you are stuck. If it has something to do with orientation or direction of the ship, check posts like this one (look for direction):https://hutonggames.com/playmakerforum/index.php?topic=21314.msg93545#msg93545

Quote
But I don't want to compare against Input, because when no keys are pressed (Input) I get a value of Zero. If the value is Zero I get no boost.

You can decide what should happen when the input is zero. That's exactly the setup above. Above returns either -1 or 1. That's the idea behind a state machine. The input only drives which states you are in, and what happens there is whatever you want there to happen.

Of course, in my setup we just decide that the first time (and input is zero), we return 1, and after that, flip to either 1/-1. But again, you can set anything.
« Last Edit: June 26, 2020, 03:57:49 PM by Thore »