#Issues with swimming

1 messages · Page 1 of 1 (latest)

fallow mist
#

thread

torpid wigeon
#

k

#
    void ApproachTarget(Vector3 targetPos)
    {
        // if distance between us and the object is small enough, stop moving towards it
        if (Vector3.Distance(koiTransform.position, targetPos) < stopDistance)
        {
            return;
        }

        // adjust our transform so we get closer every frame if applicable
        rb.velocity = koiTransform.forward * Time.deltaTime * followSpeed;
    }```
fallow mist
#

so what do you expect to work?

torpid wigeon
#

This is what I use to have them move toward the point.

fallow mist
torpid wigeon
#

I set their follow speed to "5".

#

They seem to swim at a decent pace when there's no pellet present.

#

But when a pellet is present and they want to go swim toward it, they seem to do so very slowly.

torpid wigeon
#

......... wwaaaaaaaaiiiiiit.

fallow mist
#

it's not koiTransform?

torpid wigeon
#

🤔

torpid wigeon
fallow mist
#

I see, so what is pellet?

torpid wigeon
#

That's the position toward which they swim.

#

In this case, it'd be the pellet's transform when one is present.

fallow mist
#

I see, if not?

#

Issues with swimming

torpid wigeon
#

Then they swim to a randomly-assigned location, as defined here:

    void Seek()
    {
        if (swimPointSet && !pellet && pelletPos == swimPoint)
        {
            swimPointSet = false;
        }
        if (!swimPointSet)
        {
            SearchSwimPoint();
        }
        else
        {
            Vector3 swimPointDist = transform.position - swimPoint;
            float travelMag = swimPointDist.magnitude;
            RotateToTarget(swimPoint);
            ApproachTarget(swimPoint);
            if (travelMag < stopDistance)
            {
                swimPointSet = false;
            }
        }
    }```
#

When it calls SearchSwimPoint which was that very first function I posted, that's where it determines which point it'll swim toward next.

#

But in any case, I think this slow move speed is more of a nothingburger.

#

I took a hard look at my fish again and found they did move just as slow when idly swimming.

#

It was as consistent when I tripled their speed.

fallow mist
#

so it's moving too slowly when pallet != null ?

torpid wigeon
#

No.

#

I'm saying that I don't think it's an issue.

fallow mist
#

But when a pellet is present and they want to go swim toward it, they seem to do so very slowly.

torpid wigeon
#

It was a matter of perception. Or... misperception.

#

So now we have one issue to solve.

fallow mist
torpid wigeon
#

No.

#

The other.

#

Where for some reason, the pellet's position is different from the swim position when a pellet is present.

#

What I had attempted to do was assign the pellet's position as a swim point if a pellet is present.

fallow mist
#

well, if I remember correctly, you're slowly swiming to the pellet's position when it's present

torpid wigeon
#

Look here again:

    void SearchSwimPoint()
    {
        //return a random point within range
        RaycastHit info;
        float travelRange = Physics.SphereCast(koiTransform.position, pellet && waterVol.bounds.Contains(pelletPos) ? Vector3.Distance(pelletPos, koiTransform.position) : swimPointRange, Vector3.zero, out info, 0f) ? info.distance : pellet ? Vector3.Distance(pelletPos, koiTransform.position) : swimPointRange;
        do
        {
            swimPoint = pellet && waterVol.bounds.Contains(pelletPos) ? pelletPos : koiTransform.position + new Vector3(Random.Range(-travelRange, travelRange), Random.Range(-travelRange, travelRange), Random.Range(-travelRange, travelRange));
        }
        while (!waterVol.bounds.Contains(swimPoint) && !(pellet ? Vector3.Distance(swimPoint, pelletPos) < Vector3.Distance(koiTransform.position, pelletPos) : false));
        swimPointSet = true;
    }
fallow mist
#

so logically, swimPos and palletPos won't be the same all the time

torpid wigeon
#

Look at "Swim Point" and "Pellet Pos".

#

They should be the same.

#

For some reason they're not.

fallow mist
torpid wigeon
#
swimPoint = pellet && waterVol.bounds.Contains(pelletPos) ? pelletPos : koiTransform.position + new Vector3(Random.Range(-travelRange, travelRange), Random.Range(-travelRange, travelRange), Random.Range(-travelRange, travelRange));```
fallow mist
#

this is false

pellet && waterVol.bounds.Contains(pelletPos)
#

so in your case waterVol.bounds don't contain pelletPos or pellet == null

torpid wigeon
#

If the pellet is missing.

#

The pellet needs to exist and its position needs to be contained in the water.

fallow mist
#

what doesn't make sense to me is that how can pelletPos be not null if pellet is null ?

torpid wigeon
#

So, if the pellet exists and it contains the pellet's position within the water, then it uses pelletPos as the next swimpoint.

torpid wigeon
fallow mist
#

that's the answer

torpid wigeon
#

Indeed.

#

I gotta be doing something wrong.

fallow mist
#

if pellet becomes null, palletPos is still its previous value

torpid wigeon
#

Well yes, but that's not the problem.

#

The problem is that when the pellet is not null, they will swim toward (0, 0, 0).

fallow mist
#

that's the problem, why do you have pelletPos vector if pellet is null..

torpid wigeon
#

Only if a pellet had already existed.

#
    void Seek()
    {
        if (swimPointSet && !pellet && pelletPos == swimPoint)
        {
            swimPointSet = false;
        }
        if (!swimPointSet)
        {
            SearchSwimPoint();
        }
        else
        {
            Vector3 swimPointDist = transform.position - swimPoint;
            float travelMag = swimPointDist.magnitude;
            RotateToTarget(swimPoint);
            ApproachTarget(swimPoint);
            if (travelMag < stopDistance)
            {
                swimPointSet = false;
            }
        }
    }
fallow mist
#

well, this one should work if it exists and is contained by bounds swimPoint = pellet

torpid wigeon
#

Using this, it clears swimPointSet so that if the pellet is gone, but the pellet's position is still equal to the pellet's last position, it'll attempt to look for a new place to swim.

#

Specifically:

        if (swimPointSet && !pellet && pelletPos == swimPoint)
        {
            swimPointSet = false;
        }
fallow mist
#

well, I still don't get what you're trying to achieve

torpid wigeon
#

Once the pellet is gone, I want the fish to continue swimming elsewhere.

#

They could look for a new pellet if one is present, or swim idly.

#

Looking for new random points toward which they can swim.

fallow mist
#

so you want:

if (pellet || GetPellet())
{
    move to pallet slowly using lerp and coroutine
}
else
{
    just swim as they want
}
#

if so, then you don't really need som much code?

torpid wigeon
#

Pretty much.

#

Only question is how do I get them to do one or the other.

fallow mist
#

and those ? : make your code very hard to understand for someone who is not you

torpid wigeon
#

If their swim point destination does not match the pellet's last known position, they'll continue swimming toward that one position.

fallow mist
#

i don't get it..

#

what about koiTransform?

#

it's so messed up

#

also koiTransform, what's even that?

#

it's targetPos

#

then what's pelletPos?

#

well, I don't think you need them both

fallow mist
#

targetPos = pellet ? smth : smthElse

#

that should be all

#

you're probably overthinking it, just make it swim to the target position if (smth) or just not to swim

#

I am sorry, I have to go now, I will be able to help you if you don't solve it tomorrow

torpid wigeon