#Bug in a snake style game

1 messages · Page 1 of 1 (latest)

rough forge
#

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.

clear currentBOT
rough forge
ripe magnet
rough forge
#

lemme try to do smth about that

ripe magnet
#

and the way you're moving the segments is just.. not how lerp works

rough forge
#

as you can tell im not very good at this

ripe magnet
#

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.

rough forge
#

wait so i dont really understand whats wrong with my lerp function

ripe magnet
#

you're ju sing it wrong

rough forge
#

there's a start end and time

ripe magnet
#

no there isn't

rough forge
#

really?

ripe magnet
#

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

crude sphinx
ripe magnet
#

yes, there

crude sphinx
#
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

rough forge
#

making a game is pretty confusing

ripe magnet
#

who said it wasn't

rough forge
#

5 year old me

ripe magnet
#

well 5 year old you was wrong about it kekw

#

game development is hard

crude sphinx
#

it is super hard

#

but super rewarding

ripe magnet
#

but if you try to tackle it one thing at a time, you can do it

rough forge
#

ok so i fixed the time part of the lerp thing but the tail is still kinda freaking out

#

but much smoother

ripe magnet
#

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

crude sphinx
#

sphere

ripe magnet
#

well yea

crude sphinx
ripe magnet
#

kekw but they're not touching the z component here, it's 2d

crude sphinx
#

then why Vector3

rough forge
#

i like it

rough forge
ripe magnet
#

uh

#

direction doesn't go farther than 1 meter (unit) away from 0, 0, while position goes as far as it wants

rough forge
#

and the problem with that is?

ripe magnet
#

that you're trying to set a position to be a direction

rough forge
#

ah

ripe magnet
#

so it can't move past 1 unit away from the origin

crude sphinx
#

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

rough forge
#

ic

#

ok lemme try to figure something out

rough forge
#

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

ripe magnet
#

what exactly do you mean by "a different lerp function"?

rough forge
#

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

ripe magnet
#

well you can just calculate the place that specific segment needs to go

#

(for example based on the previous segment's position)

rough forge
#

But don't I need an index to do that

ripe magnet
#

yea

#

just use a for loop

#

instead of a foreach

rough forge
#

Ok

rough forge
ripe magnet
#

which part are you struggling with?

rough forge
#

calculating the position which each body segment is supposed to be in

ripe magnet
#

well you have the head's position

#

and an array of the previous positions

#

right?

rough forge
#

yes

ripe magnet
#

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

rough forge
#

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

#

?

ripe magnet
#

this for loop.. doessn't really make much sense

rough forge
#

makes sense

ripe magnet
#

it's gonna be the same for all segments

#

you're just setting all of their positions to be the same

rough forge
#

yes thats the problem that i was facing

#

im gonna try to fix it again

#

thx