#okay yeah, still only nudges the

1 messages · Page 1 of 1 (latest)

glacial vault
#

We should probably have made a thread long ago

#

what does the final code look like now

nocturne ravine
glacial vault
#

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

nocturne ravine
#

ye alright, must've misread your messages I'll see if this fixes it

#

changing it to this: did seemingly nothing surprisingly

glacial vault
#

so what behavior are we seeing now?

#

it's going to the right height, and in the right direction, but not going far enough?

nocturne ravine
#

I'm trying to test an enemy, that's why I need the projectiles lol

glacial vault
#

can you switch that to an MP4

nocturne ravine
#

yeah sure one sec

glacial vault
#

and it will embed in Discord

nocturne ravine
glacial vault
#

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

nocturne ravine
#

alright lemme give this a go

glacial vault
#

although to be honest, that doesn't look like a gravity of 12 to me

nocturne ravine
#

I mean it is?

glacial vault
#

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

nocturne ravine
#

okay here's what the values are giving: you seen the video and I don't think that time of flight is correct

glacial vault
#

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 😢

nocturne ravine
#

chatgpt cursed you lol

glacial vault
#

can you try - in the time of flight calculation - using -gravity every time you see gravity

#

that is certainly a possibility lmao

nocturne ravine
#

ye gimme a second

glacial vault
#

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

nocturne ravine
#

okay changing the code to this: made it go half the distance for some reason

glacial vault
#

Wait no I don't nvm

#

Did you miss the other gravity instances there?

#

There's the gravity * 2 in the middle

nocturne ravine
#

okay and also the time of flight is now 7 which would be double what it's supposed to be

nocturne ravine
#

and gave NaN

glacial vault
#

I see 🤔

nocturne ravine
#

so I tried different variations

glacial vault
#

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

nocturne ravine
#

giving that a go its loading

#

@glacial vault this worked! Thanks again for helping solve this hellscape of a problem!

nocturne ravine
#

this worked too for some reason btw:

glacial vault
#

want to see it in action 😛

nocturne ravine
#

and yeah

glacial vault
# nocturne ravine this worked too for some reason btw:

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

nocturne ravine
glacial vault
#

awesome!

nocturne ravine
#

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.

glacial vault
#

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)

nocturne ravine
#

me being above the enemy and below the enemy always works btw

glacial vault
#

nice

nocturne ravine
#

this code seems to work perfect for my needs, I don't think I need to change it?

glacial vault
#

what about when you're more than desiredHeight higher than the enemy?

#

Or is that not possible?

nocturne ravine
#

i'll try

#

hm yeah you're right, going above the desired height it does the NaN stuff again

glacial vault
#

yup! You'll need a special case.

nocturne ravine
#

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

glacial vault
#

those are certainly options

#

at this point it's more of a game design question. Have fun!