#okay yeah, still only nudges the
1 messages · Page 1 of 1 (latest)
We should probably have made a thread long ago
what does the final code look like now
oh you did the parentheses wrong
the final closing parentheses should go where I marked, not at the end
you included the / gravity bit, which shouldn't be there
the point was to have (ALL THE STUFF) / gravity
like in the formula we had
ye alright, must've misread your messages I'll see if this fixes it
changing it to this: did seemingly nothing surprisingly
so what behavior are we seeing now?
it's going to the right height, and in the right direction, but not going far enough?
can you switch that to an MP4
yeah sure one sec
and it will embed in Discord
ok so we need to debug the code now
I would start with this:
At the end of the function log some stuff:
Debug.Log($"Time of flight: {timeOfFlight}. Distance to target: {horizontalDistance}");```
See if the numbers it's spitting out make sense
based on the video the time of flight should be something around 3 ish
alright lemme give this a go
although to be honest, that doesn't look like a gravity of 12 to me
I mean it is?
fair enough
oh one other thing
make sure your rigidbody on the projectile has drag set to 0
linear drag
if it has drag that's going to completely destroy this whole calculation lol
okay here's what the values are giving: you seen the video and I don't think that time of flight is correct
that time of flight is wrong yeah
that's why it's not going far enough
it thinks it has 10 seconds to reach the horizontal distance, so it goes too slow
in reality it only has about 3 seconds
so something is wrong with our time of flight calculation 😢
chatgpt cursed you lol
can you try - in the time of flight calculation - using -gravity every time you see gravity
that is certainly a possibility lmao
ye gimme a second
BTW another interesting thing to log here might be timeOfFlightMinus and timeOfFlightPlus
If the calculations are correct the smaller of the two should be very close to zero or even negative
Oh wait
I see something else wrong too
okay changing the code to this: made it go half the distance for some reason
Wait no I don't nvm
Did you miss the other gravity instances there?
There's the gravity * 2 in the middle
okay and also the time of flight is now 7 which would be double what it's supposed to be
making those negative broke it
and gave NaN
I see 🤔
so I tried different variations
Oh wait
I think I know the issue
Revert that change
One sec lemme type this out
so basically calculatedDesiredHeight is wrong there
it's correct for the yVel calculation
but for the time of flight calculation it should be initialHeight - target.position.y
giving that a go its loading
@glacial vault this worked! Thanks again for helping solve this hellscape of a problem!
can i see a video?
this worked too for some reason btw:
want to see it in action 😛
and yeah
i think this works too because essentially we're just getting the opposite end of the parabola, and then the logic to take the larger one "fixes" it. I Actually don't think it would work if you were standing at a different height than the cannon
in any case you should test when the player is above or below the thing
You may also want to have logic where like if ((playerHeight - enemyHeight) > desiredHeight) // don't shoot at all because we can't reach
okay here
awesome!
i'll get to testing those right now, but the enemy shooting regardless of if it can hit the player is apart of the design still.
gotcha. In that case you'll have to do something special because you're going to end up with like... infinity or NaN for the TimeOfFlight calculation
so you'll have to basically decide on a horizontal velocity somewhat arbitrarily.
I guess maybe you could make the horizontal velocity equal to the vertical velocity in that case. That will give you a 45 degree angle which will result in the farthest possible shot (but it still won't hit the player)
me being above the enemy and below the enemy always works btw
nice
this code seems to work perfect for my needs, I don't think I need to change it?
what about when you're more than desiredHeight higher than the enemy?
Or is that not possible?
i'll try
hm yeah you're right, going above the desired height it does the NaN stuff again
yup! You'll need a special case.
might just get the distance of the player and either increase desired height to the original desired height + the player y position, or just make the enemy not shoot and make it try to get closer