#Object Not Thrown in the Right Direction

1 messages · Page 1 of 1 (latest)

ornate pecan
#

Well for one, hspeed and vspeed apply a current momentum, with for hspeed, positive going to the right, and going to the left

#

You would preferably want to apply hspeed after the instance is made and it's create event has executed

#

You can then use a normalized direction variable to change hspeed based on the direction of the object

if pickedUp && key_attack
{
    with(instance_create_layer(x,y - 200,"Objects",obj_carThrow))
    {
      hspeed = hspeed * dir; // Assuming that dir is a variable and that it's normalized between -1 to 1.
    }
        instance_destroy(obj_car)
}
grand sparrow
#

I got it to work,lol

ornate pecan
#

Sweet!

grand sparrow
#

but I am looking at the code you sent me too and i think yours will be alot better, but this is how i did it

#
{
        
        var car = instance_create_layer(x,y - 200,"Objects",obj_carThrow);
        car.hspeed = 30 * sign(hsp)
        instance_destroy(obj_car)
}

ornate pecan
#

That works just as fine honestly

#

sign is normalizing hsp

#

But it can introduce some errors, since hsp can be normalized to 0

grand sparrow
#

Awesome, This should be in a state machine, but i tested this out in the step even before I put everything in there. it is just a way for me to get organized

ornate pecan
#

State machines are definitely recommended for organization purposes :p

grand sparrow
#

Oh yay, that is true. i will need to organized this because this was very rough and the final is going to be alor different

#

Just one more question, when the player stands still and throws the object, the object will just drop to the ground and not in any direction. i was wondering how I can fix this issue?

ornate pecan
#

In much the same case

#

Since hsp is 0, sign(hsp) will return 0

#

And 30 x 0 is 0

#

You'll need to account for that

#

Which is why I prefer to have some input check and update a dir variable

#
var _dir = key_right - key_left;
dir = (_dir != 0 ? _dir  : dir);
grand sparrow
ornate pecan
#

dir would have to be defined in the create event

grand sparrow
#

I actually have that in the create event XDDD

ornate pecan
#

And this code would have to be within the players step event

#

Since it's being updated constantly

#

Doing it only whenever it's thrown would only update if there was some kind of input check happening at the same time

grand sparrow
#

I have this in my players input

#

however, i see you did this

#

dir = (_dir != 0 ? _dir : dir);

ornate pecan
#

Yes

#

The difference between mine and yours, other than that I used a conditional operator

#

Which translate to this basically

if (_dir != 0) {
  dir = _dir;
} else {
  dir = dir;
} 
#

Your way would have the left always override the right, even if the player was moving right and then decided to try and move left, and not vice versa the other way

#

But otherwise, it works

ornate pecan
grand sparrow
#

it is yes

ornate pecan
#

Right, and how do we update hsp?

grand sparrow
#

I think this it , in the player input

ornate pecan
# grand sparrow I think this it , in the player input

I had to get up and do some stuff, sorry for that.
The main thing you'll notice is that hsp can be set to 0, which means the check on sign(hsp) would return 0, since it's neither greater than or less than 0.
Which doesn't happen with my above example, cause dir doesn't get altered other than if _dir is not equal to 0

grand sparrow
# ornate pecan Which translate to this basically ```gml if (_dir != 0) { dir = _dir; } else {...

Where would I put this directions because right now i have in the pick up state. Also, would I have to make a dir variable in the create event of the carThrow object? ``` if pickedUp && key_attack
{
with(instance_create_layer(x,y - 200,"Objects",obj_carThrow))
{
hspeed = hspeed * dir; // Assuming that dir is a variable and that it's normalized between -1 to 1.
}
instance_destroy(obj_car)
}

var _dir = key_right - key_left;
dir = (_dir != 0 ? _dir  : dir);                
    ```
ornate pecan
#

It’s something that’s meant to be updated constantly, like your inputs

grand sparrow
#

right now, I have this in my player input, would I replace it with that?

#
        {
            dir = 1    // The plater will go into the position direction(the righrt
        }

    if key_left // If the player is pressing the left key of the analong stick
        {
            dir = -1     // The plater will go into the negative direction(the left)    
        }```
ornate pecan
#

Though that should also be fine

grand sparrow
ornate pecan
#

So, it should already have a number

#

As you had it defined in the create event already

grand sparrow
#

oh because I never used hspeed, but hsp

ornate pecan
grand sparrow
#

oh ok ok, i did try it,m but the object just drops to the ground

ornate pecan
#

Set a break point on the create event and step through it with the debugger

#

Check the value of hspeed

grand sparrow
#

im checked it and it it is saying its around 0.00 or something like that. I have no idea what is going on, lol

ornate pecan
#

What's the actual value, what's the value of dir that's multiplying hspeed, etc?

grand sparrow
#

in the create event, i put dir at 0.

ornate pecan
#

And why?

#

Actually, let's ask a better question

#

What do you think 20 x 0 does?

grand sparrow
#

it equals 0. yay..... that is the problem. why i been forgetting my multiplication XDDD

ornate pecan
#

Right, so

#

hspeed = hspeed * dir

#

Which the game would fetch and put in place

#

hspeed = 20 * 0 (pseudo, not literally)

grand sparrow
#

i see what your saying, it does work, but it can only be thrown to one side

ornate pecan
#

Right, so how would we fix that?

grand sparrow
#

im assuming sign(dir)

ornate pecan
#

no

#

I'll give you a hint.
||We already have a normalized dir variable, we just need to pass that from the object that handles picking up + throwing to our thrown car||

#

So, what else?

grand sparrow
ornate pecan
#

Yes

ornate pecan
#

What could we change here to pass the players dir to the thrown car?

grand sparrow
#

i was thinking of adding dir in the if statement, but that wouldnt work, but could add that before the with statement

ornate pecan
#

Again, no

#

We're not doing anything with if statements here

grand sparrow
#

you mean the dir for the player?

ornate pecan
#

Yes

#

We have the players dir, it is either -1 or 1

grand sparrow
#

i am trying to figure this out on my own. I know its dir, but I need to set it so that he throws it in the direction he is facing

ornate pecan
#

I'll break it down a bit

#
    with(instance_create_layer(x,y - 200,"Objects",obj_carThrow))
    {
      hspeed = hspeed * dir; // Assuming that dir is a variable and that it's normalized between -1 to 1.
    }
#

So we have a smaller sample to go off by

#

We have dir, but dir refers to the instance that we just created

#

Not the player, right?

grand sparrow
#

yaya, that is right

ornate pecan
#

So, what do we change from here to get the players dir?

grand sparrow
#

the hspeed

ornate pecan
#

In what way are we changing the hspeed?

grand sparrow
#

the direction of it

ornate pecan
#

Right

#

But what can we use to get the players dir?

grand sparrow
#

i am assuming image_xscale?

ornate pecan
#

Do you want another hint?

grand sparrow
#

yes please

ornate pecan
#

||It's involving no other variables, just hspeed and dir||

grand sparrow
#

dir = hspeed * dir?

ornate pecan
#

no

ornate pecan
#

I can give one more hint

grand sparrow
#

one more hint

ornate pecan
#

||other||

grand sparrow
#

other.obj_layer?

ornate pecan
#

Not quite

#

other is an instance keyword

#

So other in this context would refer to obj_player

grand sparrow
#

like with(other) or is this something different

ornate pecan
#

We're not using with

#

Just think about this

#

If other is obj_player

#

And we need to get dir from obj_player

#

What would that look like?

grand sparrow
#

I know i have done this before, let me think. obj_player.dir?

ornate pecan
#

Yes

#

But if we use other, it'd do the same thing, but refer to that particular instance of obj_player

grand sparrow
#

ok , would would i use other instead of obj_player.dir?

ornate pecan
#

Yes

#

other.dir

grand sparrow
#

ohhhhhh

ornate pecan
#

I need to ask, how familiar are you with GML?

#

And GameMaker, as a whole?

grand sparrow
#

i been using it for a couple of years

ornate pecan
#

Right, but how familiar?

grand sparrow
#

I am still learning the software, ive made a couple of games and they are posted on itchio

ornate pecan
#

Hmm, well I'm thinking that in the future it might be best to ask these sort of questions in r/GameMaker (GameMaker's Discord server)

#

Given that you're still learning

grand sparrow
#

oh yay, sorry about the long discussion about this,lol

ornate pecan
#

Since we're a library server, and a lot of things we do have here require a lot more of knowing GML and the fundamentals

#

We're happy to help if it's a library specific question you've got

grand sparrow
#

I still have one more question, now that its other.dir,m would I put that with the hspeed = hspeed * dir?

ornate pecan
#

Yes

#

It'll end up as

#

hspeed = hspeed * other.dir

grand sparrow
#

ok cool cool. the object disappears for some reason, but I will try to fix that

ornate pecan
#

Probably going way too fast

#

I'd suggest adjusting hspeed to a lower value

grand sparrow
#

cool cool. thank you so much for the help

ornate pecan
#

No worries