#code explained
Explore tagged Tumblr posts
milkshakebattlecat · 8 months ago
Text
Tumblr media
I dislike using rigidbodies to move my objects because physics interactions can sometimes go ham and while that can be very amusing, I prefer things to be predictable. So for moving arrows in this game I handled the movement math myself via coroutine. Let's take a look-see, shall we? :3
Tumblr media
The goal of this coroutine is to move its symbol object in an arcing motion from its initial position, moving it upward and to either the right or left. Then it will fall downward. Rather than having each symbol object run this coroutine from an attached script, I am using a central script (my GameManager) to apply this movement to a given object, so the first thing I do is make sure the symbol still exists before proceeding with the coroutine:
Tumblr media
If we find that our symbol has been destroyed, we exit the coroutine with "yield break". You wouldn't need this check if the script is running this movement on its own object, as coroutines are ended upon an object's destruction.
There are a bunch of variables we'll define within our coroutine to calculate our desired motion; we'll start by defining an arcDuration:
Tumblr media
This determines how long the object will take to move in the arc shape. A shorter duration results in faster movement. Using a random amount between a min and max duration creates some variance in how fast different symbol objects will move. I have my minArcDuration set to 1 and maxArcDuration set to 2.5 for quick bouncy movements.
Tumblr media
These variables referencing the outermost bounds of the camera's view will be used to ensure that symbols remain within the visible area of the camera at all times. I'm not using a topBound because I'm fine with symbols possibly going off the top of the screen, but I use a maxArcHeight variable that is set low enough that they never do.
Tumblr media
For even more spawn variability, we add a little randomness to our starting point. My spawnPointVariance is set very low at 0.3; my initial symbol spawn position is low on the screen, and due to how the rest of this coroutine works, it's important that the symbols are never allowed to spawn below the bottomBound or else they will be instantly deleted (and result in a miss!)
Tumblr media
The height here is, of course, how far up the symbol will travel, and the distance refers to how far it will move to the left or right. We calculate the peak of the arc by adding our distance and height to the x and y values of our starting position. Randomizing between negative and positive distance values for our x position adds another layer of variability which includes the possibility of moving either left or right, even though our minArcDistance and maxArcDistance are both set to positive values for clarity (mine are set to 1 and 6).
Tumblr media
This is the part of the code that decides upon our symbol's speed by calculating the distance it has to cover from its start to its peak. By dividing our horizontalDistance by our arcDuration (distance divided by time), we calculate how fast the symbol needs to move to cover the entire distance in the given duration. Mathf.Abs is used to ensure that horizontalDistance is always positive, lest we get a negative value that causes us to move in the opposite of the intended direction.
Tumblr media
We'll also want a speed variable for when the arcing motion ends and the symbol starts falling, that's where downwardSpeed comes in. In earlier versions of this function, I used downwardSpeed alone to transform the object's position, but I've since refined the logic to take the current horizontalSpeed into account for more consistent motion; we'll see that later. (Also you can see I've been tweaking that arbitrary range a bit... the fall speed was brutal during those mass waves ;o;)
Tumblr media
Here we create an elapsedTime variable starting at 0. In our while loop, we will use this variable to count how much time has passed, and if it becomes greater than or equal to arcDuration, we'll change isFalling to true and begin moving down.
We create a Vector3 moveDirection which gives the vector pointing from the startPosition to the peakPosition, and then turn it into Vector3 horizontalDirection, which retains only the X-axis direction. Both values are normalized to ensure consistency. Without normalization, the magnitude (or distance) of the vector would vary depending on the distance between the start and peak positions, which could result in inconsistent speed. Normalization caps the magnitude at 1, meaning the vector represents just the direction, not the distance, allowing for consistent speed calculation later.
Tumblr media
Here's how we start our while loop: as long as our symbol object is not null and the game says we canMove, we say yield return null, which will instruct our loop to occur every frame. If either the symbol becomes null or canMove becomes false, the while loop will end and so will the coroutine - for this reason, I only set canMove false when the game ends and the symbols will never have to resume movement, rather than in cases where I want them to pause movement and resume later, such as when a player pauses the game or during level-up periods. For the latter I use an isLevelingUp bool in my while loop that waits until that bool is false before proceeding (yield return new WaitUntil(() => !isLevelingUp)), and for the former I actually change the game Time.timeScale to 0, which is not typically recommend but fuck it we doin it live, because I don't have a mechanism for resuming this function with appropriate variables if it is stopped. It could surely be done if you just store the local variables somehow.
Tumblr media
This is the first part of our movement logic that we put in the while loop; remember we already set isFalling false, so this part will proceed with the rising motion.
Tumblr media
We count our elapsedTime here by adding Time.deltaTime, a variable which represents the time in seconds that has passed since the last frame, ensuring that time calculation is frame-rate independent. Do NOT use Time.time in cases like this unless you want your users with varying computer specs to all have different experiences with your game for some insane, villainous reason
The variable 't' is looking at the elapsedTime divided by arcDuration, a ratio that tells us how far along we are in the arc movement. If elapsedTime equals arcDuration, this ratio would be 1, meaning the arc is complete. We use Mathf.Clamp01 to clamp this value between 0 and 1, ensuring that it won't ever go higher than 1, so that we can use it to calculate our desired arcPosition and be sure it never exceeds a certain point due to frame lag or some such. If 't' is allowed to exceed 1, the arcPos calculation could possibly go beyond the intended peakPos. We are going for predictable motion, so this is no good
Tumblr media
We define our Vector3 arcPos with Vector3.Lerp, short for "Linear Interpolation", a function for calculating smooth transition between two points overtime. Ours takes our startPos and peakPos and moves our symbol between the two values according to the value of 't' which is incrementing every frame with Time.deltaTime. As 't' progresses from 0 to 1, Vector3.Lerp interpolates linearly between startPos and peakPos, so when 't' is 0, arcPos is exactly at startPos. When 't' is 1, arcPos reaches peakPos. For values of 't' between 0 and 1, arcPos is smoothly positioned between these two points. Very useful function, I be lerping for days
Then we alter the y coordinate of our arcPos by adding a calculation meant to create smooth, curved arc shape on the y axis, giving our object its rounded, bouncy trajectory. Without this calculation, you'll see your symbols rising and falling sharply without any of that rounded motion. This uses some functions I am not as familiar with and an explanation of the math involved is beyond my potato brain, but here's a chatgpt explanation of how it works:
Mathf.Sin(t * Mathf.PI): This calculates a sinusoidal wave based on the value of t. Mathf.PI represents half of a full circle in radians (180 degrees), creating a smooth curve. At t = 0, Mathf.Sin(0 * Mathf.PI) is 0, so there’s no vertical displacement. At t = 0.5, Mathf.Sin(0.5 * Mathf.PI) is 1, reaching the maximum vertical displacement (the peak height of the arc). At t = 1, Mathf.Sin(1 * Mathf.PI) returns to 0, completing the arc with no vertical displacement. This scales the vertical displacement to ensure the arc reaches the desired height. If height is 10, then at the peak, the symbol moves 10 units up.
Tumblr media
With those positions calculated, we can calculate the "newX" variable which represents where we want our symbol to appear along the x axis. It adds the horizontal movement to the current x coordinate, adjusted for the time passed since the last frame.
We use Mathf.Clamp to ensure our newX value doesn't exceed either the left or right bounds of the screen. This function limits the given value to be between min and max value.
Tumblr media
Finally we tell our loop to actually reposition the symbol object by creating a new Vector3 out of newX, arcPos.y, and using our symbol's own z coordinate. That last bit is important to ensure your sprite visibility/hierarchy doesn't go out of whack! If I used arcPos.z there instead, for example, it's likely my sprites would no longer be visible to my camera. The z position the symbol spawned at is the z position I want it to retain. Your needs may vary.
Tumblr media
This part tells us our arcDuration should end, so we set isFalling to true, which will cause the secondary logic in our while loop to trigger:
Tumblr media
Previously, objects retained their x position and only had negative downwardSpeed applied to their y position, but I didn't like that behaviour as it looked a little wonky (symbols would reach their arc peak and then suddenly stop and drop in a straight line downward).
Tumblr media
By creating a new Vector3 fallDirection that retains the horizontalDirection and horizontalSpeed from the arc phase, we're able to apply smooth downward motion to the symbol that continues to the left or right.
Just below that, we once again clamp the symbol's x position to the left and right screen bounds so the symbols can't travel offscreen:
Tumblr media
The loop would continue causing the symbols to fall forever if we didn't have this check:
Tumblr media
which triggers some project specific logic and destroys the symbol, then exits the coroutine with "yield break". Although the coroutine already exits when the symbol becomes null, which it will see is the case in the next frame as we destroyed the symbol here, adding an explicit yield break is an added layer of security to ensure predictability. Once again, not super necessary if you decide to run this code from the moving object itself, but just be sure you move your Destroy() request to the bottom of any logic in that case, as nothing after that point would be able to trigger if you destroy the object which is running the coroutine!
and that's all folks. If this helps you make something, show me!
HEY, did you really make it all the way to the end of this post?! ilu :3 Do let me know if this kind of gratuitous code breakdown interests you and I will summon motivation to make more such posts. I personally like to see how the sausage is made so I hoped someone might find it neat. If ya got any questions I am happy to try and answer, the ol' inbox is always open.
3 notes · View notes
fence-time · 6 months ago
Text
Tumblr media Tumblr media Tumblr media Tumblr media
5K notes · View notes
monkesupreme · 6 months ago
Text
Tumblr media Tumblr media
cat related revelations...
Tumblr media
bonus- panels wo lighting:
Tumblr media Tumblr media
5K notes · View notes
cthulhum · 1 year ago
Text
does anyone realize how crazy it is to have the actor of a mostly headcanoned queer ship say the fans were never crazy and they were right all along after 10+ years of everyone just absolutely going nuts over the said queerbaited ship
3K notes · View notes
faunandfloraas · 5 months ago
Text
Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media Tumblr media
Teacher Chan and his pupils 🐧
555 notes · View notes
ivoryratdoggerythethird · 1 year ago
Text
dazai making up a whole language with fyodor that no one else can understand is amazing but imagine him using codes that, very objectively speaking, you could crack, it's just that no normal person would ever make the insane leaps in logic that it requires. except for someone familiar with dazai's weird thinking patterns, that is.
i just love the idea of dazai's unhinged antics being dialed up to an eleven when he was in the port mafia, and oda being the only one who simply wouldn't bat an eye at it but chuuya was the only one who would actually get it.
like imagine ango at the end of the jailbreak, his boss saying he should allow himself to sigh and lean back and maybe indulge himself, pat him on the shoulder, tell him what he pulled off reading heart rates wasn't easy and he should be proud for being able to keep up with such a plan
but ango i-drank-with-teenage-dazai-and-also-had-the-records-for-every-soukoku-mission sakaguchi can only remember the time dazai was like using greek sign language through his breathing patterns to communicate from a submarine from beneath the pacific ocean or something, and chuuya could not fathom how no one else could understand him.
and that was the day mori signed off on skk being exclusive partners because every subordinate in the room was crying tears of blood by the time chuuya finished explaining which blood pressure level was warning them about a bomb, which blinking sequence was him conveying the vault password and which series of inhales was just him calling mori a bitch.
(ango also pointedly did not want to think about how smug dazai had looked after the mission when mori confirmed skk would only be each others' partners for efficiency and to maintain everyone else's sanity
or about how when he called chuuya to tell him about dazai's prison break scheme he could only get like 3 out of 276 steps into the plan before chuuya rolled his eyes, said "got it" then hung up and pulled the whole thing off without a hitch.)
2K notes · View notes
critai · 2 years ago
Text
Tumblr media
4321
7K notes · View notes
phantomrose96 · 9 months ago
Text
"Sorry boss it's just been a really hard day to focus at work. Yeah it's just every single person in the group chat knows how we could have fixed BNHA."
909 notes · View notes
valtsv · 10 months ago
Text
dragon and their rider both posting photos of each other captioned "when you're mean to me this is who you're being mean to" but with very different implications
578 notes · View notes
somewhereincairparavel · 4 months ago
Text
jason grace is so american but not american at the same time. I feel like considering how sheltered he and the other romans were in new rome, I'm surprised of his constant usage of ‘dude/man/bro’
247 notes · View notes
flimsy-spine · 5 months ago
Text
Tumblr media Tumblr media
professional silly guy, chimney han
314 notes · View notes
prettyydeadly · 10 months ago
Text
Tumblr media
"Thank you for the toxic yuri RJ" we all say in unison
594 notes · View notes
buglaur · 1 year ago
Text
Tumblr media Tumblr media Tumblr media
sims character page code
hii, ages ago i made a simple but customisable character page to try and get the hang of html and css, so i thought i'd release it. all of the instructions are within the code! the code will look like the first pic when you put it in your page but you can play around with it to get different looks. i tried to make it as beginner friendly as possible so even if you have no code knowledge you should be able to follow it :) but if not, don't hesitate to send me an ask!
preview | download code
1K notes · View notes
idontmindifuforgetme · 7 months ago
Note
sorry if this is a weird question, but i was wondering if you knew respectful words in arabic when talking to older women? ive been talking with Palestinian women in Gaza, and it always feels wrong or rude to call them by first name esp since im young. maybe its just the east asian in me but was curious if theres any cultural respect norm that i should know here since its hard to find online
OHH this is tough bc the Iraqi in me grew up referring to older women by “khala” or “ama” which is literally just the Arabic word for aunt …….. and most times it goes well but sometimes women take offense to it bc they’re like I’m NOT that old lmao I love Arab women
288 notes · View notes
lambmotifz · 7 months ago
Text
sam’s demonic blood being framed as a symbol of impurity. dean’s madonna/whore complex and his adoration of “sammy” as the image of the ideal little brother(wife). sam getting the whore treatment during the demon blood addiction era
sam is very much female coded and the show couldn’t be more unsubtle about it
216 notes · View notes
cutter-kirby · 14 days ago
Text
gregor the underland chronicles is literally so fundamental to me. this boy is a natural born killer. trained to be a martyr. and he is kind. he is angry. he cares so much for the unloved. he is always willing to give people a chance, so aware of how others can change. the warrior and son of the sun. the intersection of protection and destruction. and he is 11/12 years old.
129 notes · View notes