#Help with instantiate slerp

1 messages ยท Page 1 of 1 (latest)

mighty hedge
#

test

#

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

arctic trench
mighty hedge
#

oh

#

1 sec

#

i'll narrow it

arctic trench
#

why do you use InvokeRepeating to begin with?

mighty hedge
#

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

arctic trench
#

your code does not have anything for the learp jet but that is the second thing we probably should tackle

mighty hedge
#

and there they slerped the chickens to place

#

so we need to work out the instantiate per 0.2 seconds first

arctic trench
#

the best way to handle the delayed spawning would be to use A coroutine to delay the spawning of the chickens

mighty hedge
#

the thing is"chicken" is a local veriable

#

so i need to make a veriable for it first right?

arctic trench
#

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

mighty hedge
#

mmm

#

but i want it to relate to the number of instantiates it needs

#

will it do the same?

arctic trench
#
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());

mighty hedge
#

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

arctic trench
#

code is probably easier
and it probably Lerp not Slerp
slerp is for spherical learps

mighty hedge
#

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

arctic trench
#

"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^^

mighty hedge
#

lerp takes to points and make movement from start to end

#

two*

#

slerp takes 2 points and a center point

#

and spherically moves it

arctic trench
mighty hedge
#

it worked for me

arctic trench
#

Slerp(Vector3 a, Vector3 b, float t);
where is the center there?

mighty hedge
#

you make the center

#

center = 2 points /2

#

and then insert it

#

i'll find the video and send it

#

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?

odd quest
#

Too long, what's the TLDR?
How to make a Slerp?

mighty hedge
#

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

odd quest
#

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.

mighty hedge
#

1 second

#

i'll find something that clarify it

#

look at how they enter the screen

#

into their place on a grid

odd quest
#

Yeah

mighty hedge
#

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

odd quest
#

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.

mighty hedge
#

but where do i put it on the code

#

in the wavespawner or in the chicken

odd quest
#

So it depends on how you designed your code

mighty hedge
odd quest
#

I don't think a spawner should do movement.

mighty hedge
#

the spawner has a movement of left and right

#

and the position the chicken need to go changes each frame

odd quest
#

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.

mighty hedge
#

"over over powered"?

#

;D

odd quest
#

Object Oriented Programming

#

You also have DOP, data oriented programming

mighty hedge
#

yea i figured it wasn't my thought

odd quest
#

DOTS is DOP

#

The movement script variant is more DOP then OOP, so I suggest that you do the movement in the chicken.

mighty hedge
#

ok

#

so i need a reference to where the chicken heads right?

odd quest
#

I also suggest reading up on both styles, OOP for sure. That's the most common one.

mighty hedge
#

so i need the row and column position

odd quest
#

Yeah, the spawner sets the start and end for the chicken and gives it to the chicken script.

mighty hedge
#

ok i'll try and update you

odd quest
#

I'm not here all day but you can try

mighty hedge
#

yea im gone in 2 hours as well

#

i will try to make it quick

odd quest
#

๐Ÿ‘

mighty hedge
#

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

odd quest
mighty hedge
#
            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

odd quest
mighty hedge
#

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

odd quest
mighty hedge
#

the father is the one going left

odd quest
mighty hedge
#

yea

odd quest
mighty hedge
#
                gridPosition.x += column * 1.5f;
                chicken.transform.position = gridPosition;

odd quest
#

๐Ÿค”

odd quest
mighty hedge
#

yea

#

inside update

#

do you want me to send you the full scripts of both?

mighty hedge
#

maybe you'll see something

odd quest
#
                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?

mighty hedge
#

it is the grid

odd quest
#

Who is setting the destination of the chicken then ๐Ÿ˜›

mighty hedge
#

the gird

#

grid

#

grid.position

odd quest
#

Either you don't understand how scripts work, or I don't understand your script ๐Ÿ˜›

mighty hedge
#

there i cancel the lerp

#

and they snap to position

odd quest
#

Show both scripts then

mighty hedge
#

1 second

mighty hedge
odd quest
mighty hedge
#

maybe grid.position is not the position at all

#

and i was mistaken

odd quest
#

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

mighty hedge
#

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

odd quest
#

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

mighty hedge
#

so can you help me with that ? im a bit lost tbh

odd quest
#

Yeah it is difficult to explain. It has to do with value types and reference types. Also something to read up upon ๐Ÿ˜›

mighty hedge
#

im new to unity, i got the coding fast but i lack information

odd quest
#

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);
            }
mighty hedge
odd quest
#

endPos = spawner.gridPosition;

#

That

#

Anyhow, I got to go to the supermarket for a second, brb.

mighty hedge
#

doesn't work

#

oh wait

#

nope

odd quest
#

What is it doing now then?

mighty hedge
#

1 sec

#

they still go to the same loaction

odd quest
#

Show the code that you made in the start

mighty hedge
#
        chickenHP = 500;
        //Lerp
        startPos = new Vector3(8, -6);
        ```
odd quest
#

Thats good

#

Now show the Lerp

mighty hedge
#
    {
            elapsedTime += Time.deltaTime;
            float percentageComplete = elapsedTime / desiredDuration;
            transform.position = Vector3.Lerp(startPos, spawner.endPos, percentageComplete);```
odd quest
#

spawner.endPos

#

No, go to the endPos of the chicken

#

Not of the spawner

mighty hedge
#

im trying

#

ok

#

they go to the same location

#

but the grid's location

odd quest
#

Huh?

#

Debug.Log(endPos); in that update for me pls

mighty hedge
#

debug says (-0.8,4,0)

odd quest
#

For all of them?

mighty hedge
#

yea

#

both are in the same location

odd quest
#

Show me your spawn code then

mighty hedge
#

its as if they take the location of the father at the spawn time

#

but dont link to the father's movement

odd quest
#

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

mighty hedge
#

i did set it

odd quest
#

Well that would create 2 different ones.

mighty hedge
odd quest
#

So do you overwrite endPos somewhere in your script?

mighty hedge
odd quest
#

No, your Chicken has a different endPos now, the code you wrote told it to.

#

So something is wrong with the code

mighty hedge
#

ok so there are 2 end pos

#

one in the chicken and one in the wave

odd quest
#
                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

mighty hedge
#

that's in wave

#

that's in chicken

odd quest
mighty hedge
#

1 sec

odd quest
mighty hedge
odd quest
#

new Vector3(centering.x + (column * 1.0f), centering.y + (row * 1.0f), 0.0f);

odd quest
mighty hedge
#

yes

odd quest
mighty hedge
#

what if

odd quest
#

Show the code you wrote for that part again...

mighty hedge
#

we make the parent move unrelated to the chicken spawns?

odd quest
#

It is 2 for loops.

#

It should have 2 different values

mighty hedge
odd quest
#

It doesn't have the column

mighty hedge
#

what do you mean

#

i pasted what you wrote

odd quest
#

new Vector3(centering.x + (column * 1.0f), centering.y + (row * 1.0f), 0.0f);

#

Its not the same?

mighty hedge
#

no

#

centering.x + (column * 1.0f)

#

this is the difference

odd quest
#

Yeah, add that

mighty hedge
#

it is added

odd quest
#

It should give the 2 chickens a different endPos

mighty hedge
#
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?

odd quest
#

Yeah

#

It would be 0,0,0 and 1,0,0

mighty hedge
#

do you want a zoom call so you can see and take over ?

odd quest
#

No I still need to go to the supermarket

mighty hedge
#

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*

odd quest
#

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

mighty hedge
#

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

odd quest
#

Glad it works now, don't know why column doesn't work this way then. That should also get upped every for loop.

mighty hedge
#

yes but i instantiated 2 chickens when testing

#

so it could'nt go to the next columun

#

because it was
1,1,0,0
0,0,0,0
0,0,0,0

#

something like that

#

it counts rows first

#

from left to right

#

@odd quest

#

the thing is, now they stay in place