#eat
1 messages Β· Page 1 of 1 (latest)
That's incredibly strange
the tleeporting
I'm using almost exactly the same code
Oh yeah... sorry XD
no its fine lol π
{
GameObject currentObject = collider.gameObject;
Vector2 placeYouWantToGo = transform.position;
Vector2 pos = currentObject.GetComponent<Edible>().pos;
float mag = (placeYouWantToGo - pos).magnitude;
float speed = Time.deltaTime * 8f;
float stepSize = speed / (mag * mag);
float orbitRadius = 2f;
float orbitSpeed = Time.time * 10f;
if (2 < mag)
{
pos += stepSize * (placeYouWantToGo - pos);
}
else
{
pos += speed * (placeYouWantToGo - pos);
}
Vector2 orbit = new Vector2(Mathf.Sin(orbitSpeed), Mathf.Cos(orbitSpeed)) * orbitRadius;
currentObject.transform.position = pos + orbit;
currentObject.GetComponent<Edible>().pos = pos;
}``` thats still my code
the edible is this
Pls could you write csharp after your three backticks
using System.Collections.Generic;
using UnityEngine;
public class Edible : MonoBehaviour
{
public Ability abilityToSet;
public int healAmt;
public Vector2 pos;
void Start()
{
pos = gameObject.transform.position;
}
}
```csharp
okay
Thank you it just makes it easier to read
how lol
Oh...
π
placeYouWantToGo is set to transform.position
Is this script attached to the object you want your spiralling things to go towards?
Oh okay
Immediately after putting your three backticks to start the code block just write csharp
```csharp
oh
uhh
```csharp
code stuff
```
foreach (Collider2D collider in objects)
{
GameObject currentObject = collider.gameObject;
Vector2 placeYouWantToGo = transform.position;
Vector2 pos = currentObject.GetComponent<Edible>().pos;
float mag = (placeYouWantToGo - pos).magnitude;
float speed = Time.deltaTime * 8f;
float stepSize = speed / (mag * mag);
float orbitRadius = 2f;
float orbitSpeed = Time.time * 10f;
if (2 < mag)
{
pos += stepSize * (placeYouWantToGo - pos);
}
else
{
pos += speed * (placeYouWantToGo - pos);
}
Vector2 orbit = new Vector2(Mathf.Sin(orbitSpeed), Mathf.Cos(orbitSpeed)) * orbitRadius;
currentObject.transform.position = pos + orbit;
currentObject.GetComponent<Edible>().pos = pos;
}```
there
srry
wait nvm i cant tell
yeah
Can you please debug the pos aswell as the transform.position
transform.pos of the object im eating?
Yeah
POS: (10.7, 0.0)(11.1, 1.9, 0.0)
POS: (10.7, 0.0)(12.2, -1.3, 0.0)
POS: (10.7, 0.0)(12.1, -1.4, 0.0)
POS: (10.7, 0.0)(12.0, -1.5, 0.0)
POS: (10.7, 0.0)(11.9, -1.5, 0.0)
POS: (10.7, 0.0)(11.9, -1.6, 0.0)
POS: (10.7, 0.0)(11.8, -1.6, 0.0)
POS: (10.7, 0.0)(11.8, -1.7, 0.0)
POS: (10.7, 0.0)(11.7, -1.7, 0.0)
POS: (10.7, 0.0)(11.6, -1.8, 0.0)
Oh... XD
Oh yeah that's right
oh
I forgot about the orbits
wait but howcome it works for u tho?
lol
it moves very slowly on the ground
then gets closer and slightly faster
then does a 360 spiln
and stops spinning and moves slowly onthe ground
but it did actually move towards me! :DDD
if (2 < mag)
{
pos += stepSize * (placeYouWantToGo - pos);
}
else
{
pos += mag / 2f * (placeYouWantToGo - pos);
}
I think that should fix that
what about the speed variable?
So it's teleporting because the orbit radius is too big I guess, just set it to like 0.1f or something
That if statement is purely to stop the glitching when it reaches the object so you can set the else to be whatever you want
Wdym?
well we never use it
We use it for stepSize
oh
also i think it works
its just very fast
oh uhm
it works at start
then it like moved a lil
then instantly teleports to plr
if (>>>>thisvalue<<<<< < mag)
{
pos += stepSize * (placeYouWantToGo - pos);
}
else
{
pos += mag / 2f * (placeYouWantToGo - pos);
}
Try making that value smaller
And also change the second equation to make it a bit smoother
I'll try and find a good middle ground
Try 0.1f
Awesome
your an absolute legend ommgmgmgmg yeysyeyesyssses
I'm just glad we finally made it work
So if you want it to speed up more when it gets closer
You want to edit the stepsize equation and the denominator where it's like (mag * mag)
whats pow?
power
i do now vtho π
Because 2^3 means 2 to the power of 3
So
If you increase x in Mathf.Pow(mag, x) then it will be insanely slow the further away you are but it will speed up super super quick when it's close to the object
so
would this also work
((mag * mag) * 1.5f)
and would that make faster or slower?
Yeah but that's the same as just dividing the numerator by 1.5
That would make it go slower
But it wouldn't change it's acceleration at all
okay
Of course you can always just go on desmos and write a few equations and see what you like the look of
Currently your velocity equation looks like this
π
If you draw what you want your speed to look like I can try and make an equation
okay
But bare in mind
x is equivalent to the distance you are away so you need to think of this as the velocity of the object is when you're looking at the x axis far on the right and then it speeds up as x gets closer to 0
yeah sooo
i like this number π
10000000000000000000000000000 as the numerator
π
XD
so how do i convert that into the code?
um i want it to start speeding up a little before
like
7
and then get faster and faster
π
XD
uh
If you say it works good then it works good right?
In fact I'm pretty sure that would decelerate it
And leaving it at 1 will make it move at a constant speed
That will accelerate it yeah
whatevs ill just fiddle you dont have to worry
Alright but if you want to try this
It's good because the velocity doesn't go to infinity
Hmmm there won't be much point in converting e
But if you mean in the future if you want a new equation
2.71828 but yeah
All you have to do is use mag as your x and make sure you multiply your equation by 1/mag
that for me in the future
okay
Yeah but you can just use Mathf.Exp(1)
uhh
speed / (1/(mag * Mathf.Exp(mag - 7))) just makes it orbit where it is
but not come to the person
Oh no
?
speed/(mag * Mathf.Exp(mag - 7))
oh
Honestly, just get rid of speed and put whatever number you want there
xD i put 1 and it tp directly behind me
Yes it will be very fast lmao
Oh and if it's way too slow when it's far away just add a constant to it
So you should end up with the equation
a/(mag * Mathf.Exp(b * mag - c)) + d
And changing all these variables to manipulate your velocity curve
And then the other variables like orbit radius and orbit speed should be pretty self explanatory π
well
the a
i change and it tp super far away
or right behind me
the speed/(mag*mag) works better :||\
Hey do what you want
Whatever looks good
$\frac{0.002}{e^{\left(3x-7\right)}}+0.3$
Hmmm
0.002f/(mag * Mathf.Exp(3 * mag - 7)) + 0.3f
its cause the 7 was way to big
You've got a spiralling edible now
yes π
Good luck with the rest of your game π
thankyou :DDD