So I'm making this snake style game but instead of the snake constantly moving even when no buttons are pressed, the snake moves only when some keys are pressed (WASD). Right now I've already made the movement and the grow function (makes the snake grow a tail), but I can't seem to make the tail follow the head and the tail is kinda just freaking out right now.
#Bug in a snake style game
1 messages · Page 1 of 1 (latest)
@rough forge, your attachment was automatically pasted to https://paste.myst.rs/d1m4nzmq
a powerful website for storing and sharing text and code snippets. completely free and open source.
a powerful website for storing and sharing text and code snippets. completely free and open source.
well it's not moving when you're not pressing buttons because you're not moving it while you're not pressing buttons
lemme try to do smth about that
and the way you're moving the segments is just.. not how lerp works
as you can tell im not very good at this
i can tell
Vector3.Lerp accepts 3 arguments, the start value, the end value and the time
and then it interpolates between the start and end value based on the time
Linearly interpolates between two points.
Interpolates between the points a and b by the interpolant t. The parameter t is clamped to the range [0, 1]. This is most commonly used to find a point some fraction of the way along a line between two endpoints (e.g. to move an object gradually between those points).
The value returned equals a + (b - a) * t (which can also be written a * (1-t) + b*t).
When t = 0, Vector3.Lerp(a, b, t) returns a.
When t = 1, Vector3.Lerp(a, b, t) returns b.
When t = 0.5, Vector3.Lerp(a, b, t) returns the point midway between a and b.
wait so i dont really understand whats wrong with my lerp function
you're ju sing it wrong
there's a start end and time
no there isn't
really?
lerp has a start and end value
the first and second argument
and it interpolates linearly between them based on the third argument, which ranges fr0 to 1
I believe cookie is referring to your usage of it on line 152
yes, there
foreach (Vector3 v in player.directionHistory)
{
player.bodySegments[itemCount].transform.position = Vector3.Lerp(transform.position, v, playerScript.Vel);
}
this usage makes very little sense at all
the way you used it on line 78 is correct
-# well I should say 151 and 77 respectively, your top line on the paste is not code
making a game is pretty confusing
who said it wasn't
5 year old me
but if you try to tackle it one thing at a time, you can do it
ok so i fixed the time part of the lerp thing but the tail is still kinda freaking out
but much smoother
well you're interpolating the position towards a direction
so, a position is anywhere from negative infinity to positive infinity
while a direction is a vector, whose length is 1
think of it as being constrained on a circle of radius 1 at the origin
sphere
well yea

but they're not touching the z component here, it's 2d
then why Vector3
i like it
uhh can you dumb it down a little more
uh
direction doesn't go farther than 1 meter (unit) away from 0, 0, while position goes as far as it wants
and the problem with that is?
that you're trying to set a position to be a direction
ah
so it can't move past 1 unit away from the origin
think of direction as compass heading, and position as coordinates
imagine you went to the post office, and the person was like "where's this letter being sent to?" and you just went "north"
which isn't helpful. where exactly? what address?
that's essentially what your code is doing
ok wait so i have a different question, how can i make it so that each different item in bodySegments have like a different lerp function if that makes sense
what exactly do you mean by "a different lerp function"?
so the different body segments are inside of a list right
so if i move then all of the body segments are going to be in one spot
i want it so that each different body segment has a different place where it needs to go
well you can just calculate the place that specific segment needs to go
(for example based on the previous segment's position)
But don't I need an index to do that
Ok
sorry for disturbing you but ive been trying to figure out how to do this but i just dont know how
which part are you struggling with?
calculating the position which each body segment is supposed to be in
yes
well each segment's position depends on its index
so, for segment i, it'd be the ith position in the array, where position 0 is the head's current position
so besides this for loop
if (player.bodySegments.Count > 0)
{
for (int i = 0; i < player.bodySegments.Count; i++)
{
targetPos = player.posHistory[segmentCount];
player.bodySegments[i].transform.position = Vector3.Lerp(transform.position, targetPos, player.elapsedTime / player.moveTime);
}
}
i add another for loop for each body segment to calculate the position
?
this for loop.. doessn't really make much sense
makes sense