#Object Not Thrown in the Right Direction
1 messages · Page 1 of 1 (latest)
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)
}
I got it to work,lol
Sweet!
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)
}
That works just as fine honestly
sign is normalizing hsp
But it can introduce some errors, since hsp can be normalized to 0
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
State machines are definitely recommended for organization purposes :p
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?
@grand sparrow
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);
would i put this variable in the event where i throw the object?
dir would have to be defined in the create event
I actually have that in the create event XDDD
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
I have this in my players input
however, i see you did this
dir = (_dir != 0 ? _dir : dir);
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
Is this still your current code?
it is yes
Right, and how do we update hsp?
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
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);
```
In none of the states
It’s something that’s meant to be updated constantly, like your inputs
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)
}```
Preferably yes
Though that should also be fine
for hspeed = hspeed * dir, should there be like a number for the hspeed?
hspeed is a built in variable
So, it should already have a number
As you had it defined in the create event already
oh because I never used hspeed, but hsp
oh ok ok, i did try it,m but the object just drops to the ground
Set a break point on the create event and step through it with the debugger
Check the value of hspeed
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
It's around 0.00 or at 0.00?
What's the actual value, what's the value of dir that's multiplying hspeed, etc?
in the create event, i put dir at 0.
it equals 0. yay..... that is the problem. why i been forgetting my multiplication XDDD
Right, so
hspeed = hspeed * dir
Which the game would fetch and put in place
hspeed = 20 * 0 (pseudo, not literally)
i see what your saying, it does work, but it can only be thrown to one side
Right, so how would we fix that?
im assuming sign(dir)
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?
your mean the throw state of the player when he throws it?
Yes
If we go back to this
What could we change here to pass the players dir to the thrown car?
i was thinking of adding dir in the if statement, but that wouldnt work, but could add that before the with statement
you mean the dir for the player?
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
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?
yaya, that is right
So, what do we change from here to get the players dir?
the hspeed
In what way are we changing the hspeed?
the direction of it
i am assuming image_xscale?
Do you want another hint?
yes please
||It's involving no other variables, just hspeed and dir||
dir = hspeed * dir?
no
We're scoped into obj_carThrow, we need to get the players dir
I can give one more hint
one more hint
||other||
other.obj_layer?
Not quite
other is an instance keyword
So other in this context would refer to obj_player
like with(other) or is this something different
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?
I know i have done this before, let me think. obj_player.dir?
Yes
But if we use other, it'd do the same thing, but refer to that particular instance of obj_player
ok , would would i use other instead of obj_player.dir?
ohhhhhh
i been using it for a couple of years
Right, but how familiar?
I am still learning the software, ive made a couple of games and they are posted on itchio
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
oh yay, sorry about the long discussion about this,lol
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
I still have one more question, now that its other.dir,m would I put that with the hspeed = hspeed * dir?
ok cool cool. the object disappears for some reason, but I will try to fix that
cool cool. thank you so much for the help
No worries