#Issues with swimming
1 messages · Page 1 of 1 (latest)
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;
}```
so what do you expect to work?
This is what I use to have them move toward the point.
yes, seems to be fine
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.
pallet?
......... wwaaaaaaaaiiiiiit.
it's not koiTransform?
🤔
No, it'd be targetPos.
I see, so what is pellet?
That's the position toward which they swim.
In this case, it'd be the pellet's transform when one is present.
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.
so it's moving too slowly when pallet != null ?
But when a pellet is present and they want to go swim toward it, they seem to do so very slowly.
It was a matter of perception. Or... misperception.
So now we have one issue to solve.
what issue are you talking about?
this one?
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.
well, if I remember correctly, you're slowly swiming to the pellet's position when it's present
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;
}
so logically, swimPos and palletPos won't be the same all the time
Look at "Swim Point" and "Pellet Pos".
They should be the same.
For some reason they're not.
swim point has to be (0, -0.1, 0) too?
Yes.
swimPoint = pellet && waterVol.bounds.Contains(pelletPos) ? pelletPos : koiTransform.position + new Vector3(Random.Range(-travelRange, travelRange), Random.Range(-travelRange, travelRange), Random.Range(-travelRange, travelRange));```
this is false
pellet && waterVol.bounds.Contains(pelletPos)
so in your case waterVol.bounds don't contain pelletPos or pellet == null
If the pellet is missing.
The pellet needs to exist and its position needs to be contained in the water.
what doesn't make sense to me is that how can pelletPos be not null if pellet is null ?
So, if the pellet exists and it contains the pellet's position within the water, then it uses pelletPos as the next swimpoint.
void Update()
{
Seek();
if (pellet)
{
pelletPos = pellet.transform.position;
}
rb.useGravity = !waterVol.bounds.Contains(koiTransform.position);
}
that's the answer
if pellet becomes null, palletPos is still its previous value
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).
that's the problem, why do you have pelletPos vector if pellet is null..
the smae if its null?
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;
}
}
}
well, this one should work if it exists and is contained by bounds swimPoint = pellet
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;
}
well, I still don't get what you're trying to achieve
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.
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?
and those ? : make your code very hard to understand for someone who is not you
sorry?
If their swim point destination does not match the pellet's last known position, they'll continue swimming toward that one position.
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
you really don't need this all, just store targetPos
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
Two separate functions.