#Help with instantiate slerp
1 messages ยท Page 1 of 1 (latest)
ok so i'll write here what im trying to make
im trying to slerp enemies to position on a grid
now i have the grid set up
and i thought i could do it through animation
but the animation controls the father
and currently it moves every enemy to 1 location
i tried to do the revokerepeating thing
but as you said it returns nothing
i will post the code
iti is long but i can narrow it if you'd like
need to press save
why do you use InvokeRepeating to begin with?
line 76 was Enemy chicken = Instantiate(this.Chickens[row], this.transform); at start
i want enemies to spawn each 0.2 seconds apart instead of together
and i want each enemy to slerp to its position on the grid
im basically making chicken invaders
your code does not have anything for the learp jet but that is the second thing we probably should tackle
and there they slerped the chickens to place
so we need to work out the instantiate per 0.2 seconds first
the best way to handle the delayed spawning would be to use A coroutine to delay the spawning of the chickens
the thing is"chicken" is a local veriable
so i need to make a veriable for it first right?
i would make CurrentWave a corotine (or move the whole function content to one)
that way you can just make a delay between each loop of the inner for loop
mmm
but i want it to relate to the number of instantiates it needs
will it do the same?
it was 1 function
private IEnumerator CurrentWave()
{
this.transform.position = new Vector3(0, 0, 0);
for (row = 0; row < this.rows; row++)
{
float width = 1.5f * (this.columns - 1);
float height = 2.0f * (this.rows - 5);
Vector2 centering = new Vector2(-width / 2, -height / 2);
Vector3 rowPosition = new Vector3(centering.x, centering.y + (row * 1.0f), 0.0f);
for (column = 0; column < this.columns; column++)
{
Enemy chicken = Instantiate(this.Chickens[row], this.transform);
chicken.killed += ChickenKilled;
Vector3 position = rowPosition;
position.x += column * 1.5f;
chicken.transform.position = position;
yield return new WaitForSeconds(0.2f);
}
}
yield return null;
}
this should do the trick
then you only have to call it via
StartCoroutine(CurrentWave());
i'll try
ok
that works
now i need it to slerp to place
either with code or with animation
i have a code for slerp
which i deleted xD
code is probably easier
and it probably Lerp not Slerp
slerp is for spherical learps
but i have the veriables
i want slerp not lerp
i want it to go from outside the screen in a spherical movement to place
"The difference between this and linear interpolation (aka, "lerp") is that the vectors are treated as directions rather than points in space. "
so that not what you want^^
lerp takes to points and make movement from start to end
two*
slerp takes 2 points and a center point
and spherically moves it
it worked for me
Slerp(Vector3 a, Vector3 b, float t);
where is the center there?
you make the center
center = 2 points /2
and then insert it
i'll find the video and send it
Here is a short slerp tutorial.
he made it in 3d
but works in 2d as well
"Vector3 center = (Start.Position + End.Position)*0.5
then you do relative center
start - center
end - center
then you do fractionComplete = (Time.time * start,end,fractionComplete)
and that's the time
then slerp the relative start and end over fractionComplete
makes sense? ๐
now the thing is that the code above is inside the gameManager and the movement of the chicken is in the chicken's script
so i might need to slerp it through the chicken script
anything?
Too long, what's the TLDR?
How to make a Slerp?
to understand you need to read
but long story short
i have a grid of chickens
and i want to instantiate chickens outside the screen which then will slerp to their set location on the grid
Your right btw and Maltzbier is wrong, it's a common misconception that Slerp is for rotations.
I still don't know what you are asking of me. Seems you understand what Slerp does, how to set a b and t.
So I don't understand the problem.
1 second
i'll find something that clarify it
Chicken Invaders is a long running series (since 1997) of shoot-em-up games created by InterAction studios, made in the Ultra VGA Engine. It's parodying Galaxian, Space Invaders, Star Wars, Star Trek, and Darius, where you shoot Chickens, stop their evil plans and save Earth (and humanity too).
โEvolution of Games Playlist: https://www.youtube....
look at how they enter the screen
into their place on a grid
Yeah
so im remaking chicken invaders
and i want the chickens to enter that way
my chickens currently are spawning on a grid 0.2 seconds apart
Yeah, so pick a starting point a outside the screen, ending point b where you want your chicken, set your center so you have a nice curve, and use your t to go there.
I suggest starting a MoveChicken Coroutine that fixes it, but I don't know the rest of your AI.
So it depends on how you designed your code
this is the wave spawner
I don't think a spawner should do movement.
the spawner has a movement of left and right
and the position the chicken need to go changes each frame
Yeah, either the chicken should do everything of the chicken. Or you make a movement script that does all the movement for all the gameobjects. That would be OOP.
yea i figured it wasn't my thought
DOTS is DOP
The movement script variant is more DOP then OOP, so I suggest that you do the movement in the chicken.
I also suggest reading up on both styles, OOP for sure. That's the most common one.
so i need the row and column position
Yeah, the spawner sets the start and end for the chicken and gives it to the chicken script.
ok i'll try and update you
I'm not here all day but you can try
๐
im having difficulties extracting information from the grid
@odd quest
wait im fine for now
okay so i managed to lerp them but they dont go to the grid
@odd quest
Show your code then
float percentageComplete = elapsedTime / desiredDuration;
transform.position = Vector3.Lerp(startPos, spawner.gridPosition, percentageComplete);```
{
this.transform.position = new Vector3(0, 0, 0);
for (row = 0; row < this.rows; row++)
{
float width = 1.5f * (this.columns - 1);
float height = 2.0f * (this.rows - 5);
Vector2 centering = new Vector2(-width / 2, -height / 2);
Vector3 rowPosition = new Vector3(centering.x, centering.y + (row * 1.0f), 0.0f);
for (column = 0; column < this.columns; column++)
{
Enemy chicken = Instantiate(this.Chickens[row], transform);
chicken.killed += ChickenKilled;
gridPosition = rowPosition;
gridPosition.x += column * 1.5f;
chicken.transform.position = gridPosition;
yield return new WaitForSeconds(0.2f);
}
}
yield return null;```
grid position is here
This seems fine to me, it will go to spawner.gridPosition
the thing is, the instantiated chickens are children of this gameobject so they move with it
but when i instantiate the chickens the parent moves to 0,0
So spawner.gridPosition isnt set for the chicken
They both move to the same position then?
yea
But where are you setting the chicken destination then?
gridPosition.x += column * 1.5f;
chicken.transform.position = gridPosition;
๐ค
This is chicken script yeah?
This is spawner script
No need yet
maybe you'll see something
Enemy chicken = Instantiate(this.Chickens[row], transform);
chicken.killed += ChickenKilled;
gridPosition = rowPosition;
gridPosition.x += column * 1.5f;
chicken.transform.position = gridPosition;
yield return new WaitForSeconds(0.2f);
How does this set the destination of the chicken?
it is the grid
Who is setting the destination of the chicken then ๐
Either you don't understand how scripts work, or I don't understand your script ๐
Show both scripts then
that is chicken
https://gdl.space/tukexapibi.cs wave spawner
Yeah, okay so...
What I meant was:
spawn the chicken
Get script of chicken
Set destination of chicken.
What you did was:
Spawn the chicken
Get the end position of the spawner
Lerp towards that, but you change that 1 reference after the second spawn so the first one also goes there
i tried to set the destination to the position on the grid after each instantiate
so that each enemy that spawns has another destination
which is the current destination
I understand what you are trying to do, but that is not how it is done.
There you create a reference to spawner.gridPosition.
There you reset the spawner.gridPosition
So they both go to the same space because you link them to the same variable.
What you should do is create a new destination variable and assign that to Chicken.
Not get the position in the Start of Chicken
so can you help me with that ? im a bit lost tbh
Yeah it is difficult to explain. It has to do with value types and reference types. Also something to read up upon ๐
im new to unity, i got the coding fast but i lack information
public Vector3 endPos; set this in your Spawner script, remove it from your Start
for (column = 0; column < this.columns; column++)
{
Enemy chicken = Instantiate(this.Chickens[row], transform);
chicken.endPos = new Vector3(centering.x, centering.y + (row * 1.0f), 0.0f); // This should work, you create a new variable just for this chicken
chicken.killed += ChickenKilled;
gridPosition = rowPosition;
gridPosition.x += column * 1.5f;
chicken.transform.position = gridPosition;
yield return new WaitForSeconds(0.2f);
}
i remove it from the start of the chicken right?
endPos = spawner.gridPosition;
That
Anyhow, I got to go to the supermarket for a second, brb.
What is it doing now then?
Show the code that you made in the start
chickenHP = 500;
//Lerp
startPos = new Vector3(8, -6);
```
{
elapsedTime += Time.deltaTime;
float percentageComplete = elapsedTime / desiredDuration;
transform.position = Vector3.Lerp(startPos, spawner.endPos, percentageComplete);```
For all of them?
Show me your spawn code then
its as if they take the location of the father at the spawn time
but dont link to the father's movement
chicken.endPos = new Vector3(centering.x, centering.y + (row * 1.0f), 0.0f); // This should work, you create a new variable just for this chicken
Is probably wrong then
Or you didn't set it
i did set it
Well that would create 2 different ones.
So do you overwrite endPos somewhere in your script?
maybe the problem lies here
No, your Chicken has a different endPos now, the code you wrote told it to.
So something is wrong with the code
chicken.endPos = new Vector3(centering.x, centering.y + (row * 1.0f), 0.0f); // This should work, you create a new variable just for this chicken
Can you debug log this
No 2 endPos, 1 for chicken 1, and 1 for chicken 2
i mean in the script
that's in wave
that's in chicken
Show me the code of both again then
1 sec
says the same
That's the problem then
new Vector3(centering.x + (column * 1.0f), centering.y + (row * 1.0f), 0.0f);
they both go to 0,0,0
They debug.log to 0,0,0?
yes

what if
Show the code you wrote for that part again...
we make the parent move unrelated to the chicken spawns?
new Vector3(centering.x + (column * 1.0f), centering.y + (row * 1.0f), 0.0f);
Its not the same?
Yeah, add that
it is added
It should give the 2 chickens a different endPos
new Vector3(centering.x + (column * 1.0f), centering.y + (row * 1.0f), 0.0f);
that was the last code i tried
which is the same no?
do you want a zoom call so you can see and take over ?
No I still need to go to the supermarket
or anything with video feedback
oh
then
will you be here in like 5 hours?
because i go to work in 50 minutes
i mean i should be at work by that time
๐
because solving problems on text is hard
over*
Supermarket won't take long, but I don't do video sessions. I like fixing small problems for a hobby and this one is not small anymore.
It should be simple to write a simple small spawn script that has an endPos of x and y. I have no idea why this is taking an hour to be honest.
Anyhow, gotta go now, brb
kk
i think i got what the problem is
i tried spawning 16 chickens
and after 4 chickens it moved column
the thing is that we changed a column instead of a row
new Vector3(centering.x + (column * 1.0f), centering.y + (row * 1.0f), 0.0f); this was correct but we needed to flip the row and column
chicken.endPos = new Vector3(centering.x + (row * 1.0f), centering.y + (column * 1.0f)); this is the new one which works
the problem now is that they dont move with the father
Glad it works now, don't know why column doesn't work this way then. That should also get upped every for loop.