#Rigidbody Follow function
1 messages · Page 1 of 1 (latest)
void FixedUpdate(){
//float dx = character.transform.position.x - transform.position.x;
//float dy = character.transform.position.y + 0.8f - transform.position.y;
//float t = Mathf.Rad2Deg * Mathf.Atan(dy / dx);
//if (dx < 0) {
// t += 180;
//}
//vx = ratSpeed * Mathf.Cos(Mathf.Deg2Rad * t);
//vy = ratSpeed * Mathf.Sin(Mathf.Deg2Rad * t);
//transform.Translate(new Vector2(vx * Time.deltaTime, vy * Time.deltaTime));
rigid.MovePosition(character.transform.position);
}```
the commented code works, i just need it faster
It would be simpler for you if you just set your velocity to Vector2 with vx, vy.
That is true, but not really a good solution.
Why not?
Actually, I misinterpreted what you meant. Nevermind.
I was thinking about something else.
so what the conventional unity way of doing this
i know i do most stuff not in an efficient way
There isn't. All 3 ways that I mentioned would work. If you want the object to be moved by other forces too, you should probably use add force though.
i just want whatever is going to make my fps not bad
With Rigidbody.MovePosition, it is the way explained in the documentation script example.
If you copy it without modifications, it will work.
But the example uses GetAxis to produce a direction
While you must calculate it from knowing the base and target position
Did you solve the issue with objects not getting destroyed that you discovered?
yes and that helped a little
but still i should have no issues getting high fps
this is a tiny 2d game
You're wrong. Several thousand moving and colliding physics objects is super heavy for the physics system.
But you should share your collision matrix, because I feel like there might be an issue with it.
Is this relevant to the topic of the thread?
I have limited time.
float dx = character.transform.position.x - transform.position.x;
float dy = character.transform.position.y + 0.8f - transform.position.y;
float t = Mathf.Rad2Deg * Mathf.Atan(dy / dx);
if (dx < 0) {
t += 180;
}
vx = ratSpeed * Mathf.Cos(Mathf.Deg2Rad * t);
vy = ratSpeed * Mathf.Sin(Mathf.Deg2Rad * t);
velocity = new Vector2(vx, vy);
rigid.MovePosition(rigid.position + velocity * Time.fixedDeltaTime);
``` this works
how can that be heavy, i can run factorio at 60 fps....
Basically, they have performance issue with physics and I suggested that moving the objects with physics as opposed to transform, might help improve it. And that's where the topic came from.
Saw you created a thread. I'll leave it here . . .
Pretty sure Factorio uses DOTS
im too stupid for DOTS
Then if you don't mind, I'll take it from here.
Too bad then.🤷♂️
Yes. With dots that's possible. Also, it's probably not using physics for these million of things.
Only for some of them.
We already talked about it. I'm not gonna repeat myself. If you need, read our conversation from the very start.
And you still didn't share the collision matrix. So if there is anything to improve, you're not helping with it.
ive been here many times
Where?
Does the enemy need to collide with UI or Water?
there isnt water
Or with any of the other layers?
Also, is that from the physics2d tab?
yes
Okay. Is there any change in performance?
Welp, there's just too many possible collisions.🤷♂️
im still failing to understand that
What does the profiler physics module say?
i can get the exact same performance out of a single python script
Same? Or better?
The physics module please.
its showing all the physics stuff
No it doesn't. That is not the physics module.
See profiler modules at the top left? Click and enable physics module.
It shows 0 for everything...
Ok, that's better.
Okay
You have over 5k rigid bodies.
5 dynamic
95 static colliders.
yuh
I don't know how many of them are bullets, but if it's even just 4, you need to run 20+k distance checks. That's a lot. And I'm sure there's something else going on too.
Most definitely not
If you're gonna use the normal physics system, at least make some limitations. Several hundreds of rigidbodies should be fine.
There are ways where you could optimize that without using dots(and the physics system), but non of them are beginner friendly.
and here i thought unity was my saving grace
What FPS are you reaching currently?
but csgo can reach that too right, its just as complicated
Are they spawning while the game is running, or are they all already spawned?
You're mixing up a lot of things without even understanding them. Cs go is a completely different type of game and it's definitely not developed by a newbie.
they spawn as the game is running
csgo is a 3D online game with projectile physics
mine is a 2d game with lil cute rats
Also, this doesn't look like 5k rats, so I really think there might be something else to it...
thats not 5k?
Yes, but not with thousands of potential collisions per frame.
Can't say for sure, but it doesn't look so. But if you can't tell for sure, I'd suspect that you don't completely understand what's going on in your code.
i completely deleted the list of rats
Anyways, I'm off to sleep. There are a few more things we could check(like inspecting the average frame, not a spike one) as well as checking the physics timestep.
If you want we could check that later.
With some basic optimizations, you could try it with up to maybe 1k Rigidbodies.
Creating objects is expensive, so we prefer keeping a number of them already in a pool, and then effectively Enabling/Disabling them as we "create and destroy" objects.
There is also the issue of how realistic and accurate you need the collisions; because Discrete is cheap, but will not work for high speeds etc. Dynamic is much more expensive, and if you need that, I imagine you'd need a system to control which ones are Dynamic and when.
Changing from moving by transform.Translate in Update, to Rigidbody in FixedUpdate, will probably improve the FPS slightly.
It sounds like your vision for the game is currently out of reach, but you could still make something out of this idea.
if i do FixedUpdate can i remove the * Time.deltaTime
No, and multiplication is the least of your issues.
so what does fixed do
FixedUpdate
also now that i made the rats rigid bodies, not all of the collisions work
Update runs every visual frame.
FixedUpdate runs at a fixed timestep, set in the properties of the Physics system.
By default, Time.fixedDeltaTime is 0.02
which means it runs 50 FPS.
Update will try running at the target framerate, or default (as much as possible).
Time.deltaTime can be used within FixedUpdate() because it is effectively aware it has been that precise amount of time.
While in Update(), Time.deltaTime represents the time elapsed since the last frame. (which then varies)
Can you explain or show a video of how Collision should work vs how they are now?
the bullets are colliding correctly but the saw blades are not which is weird because they run on the same system
@hollow parrot if you upload your project to GitHub and share the link, I might have a look tomorrow. I'm kinda curious how far you can push the 2d physics and why you get the results you get now.
did you give them a Rigidbody or a Rigidbody2D ?
2D and 3D physics are entirely separate systems, and will not interact with one another.
That means Rigidbody (3D) won't work with BoxCollider2D etc.
do the rats collide with one another?
that would be expensive
Actually, nevermind, they are kinematic
no they dont
it only changed like 1 line
To what?
void Update(){
float dx = character.transform.position.x - transform.position.x;
float dy = character.transform.position.y + 0.8f - transform.position.y;
float t = Mathf.Rad2Deg * Mathf.Atan(dy / dx);
if (dx < 0) {
t += 180;
}
vx = ratSpeed * Mathf.Cos(Mathf.Deg2Rad * t);
vy = ratSpeed * Mathf.Sin(Mathf.Deg2Rad * t);
velocity = new Vector2(vx, vy);
rigid.MovePosition(rigid.position + velocity * Time.deltaTime);
}```
This works?
rn im trying to figure out why the bullet collisions work but none of the other collisions do
even though they are exactly the same
this is the saw blade
this is the bullet
and the scripts are the same
Post the entire script.
If they have different names, post both.
https://gdl.space
in your sawBladeScript you have movement code in Update.
Calling transform.position will conflict with the physics system.
Rigidbodies cannot be moved by transform.position if there is supposed to be any interaction.
the same goes for Rotate, if that is supposed to be affected (or can be)
are the saw blades Kinematic?
they are dynamic
or rather, I assume the bullets are not kinematic?
both are dynamic
I am saying Kinematic, not dynamic*
what
Kinematic Rigidbody and Dynamic Rigidbody behave differently.
what do they need to be
Kinematic Rigidbodies are not affected by other objects or forces. (For example a Player rigidbody, where you script your own physics in the Controller or Motor script. )
Dynamic Rigidbodies are however only affected by the forces of physics, such as the simulation of a collision between two objects.
so i want kinematic
Kinematic moves by MovePosition()
Dynamic moves by AddForce()
Check your bullets. Are they Kinematic?
so it worked the way you wanted, when they were Dynamic
it worked the way i wanted before i made the rats rigid bodies
but after that, you said the Bullet collisions worked, but the sawBlades did not
yes
the way you solve the sawBlades, is by making them like the bullets
what is different
Kinematic or Dynamic movement
if bullets work the way you want them to, and they are Dynamic
then the sawBlades also need to be Dynamic
they were both dynamic
Your sawBlade script has something the Bullet script does not have.
Do you know what that is?
movement code by transform.Translate() inside Update
which will interfere with the physics system
bullets are moved using transform too
void Update() {
mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
x = gameObject.transform.position.x;
y = gameObject.transform.position.y;
float dx = mouseWorldPosition.x - x;
float dy = mouseWorldPosition.y - y;
t = AngleBetweenPoints(arm.transform.position, mouseWorldPosition) + 180;
arm.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, t));
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, t));
if (gameObject.name == "deagle(Clone)") {
pistol();
}
else if (gameObject.name == "shottyPrefab(Clone)") {
shotgun();
}
foreach (Bullet b in gameController.bullets) {
if (b != null) {
//b.transform.position = new Vector2(b.transform.position.x + b.vx, b.transform.position.y + b.vy);
b.transform.Translate(new Vector2(b.vx, b.vy) * Time.deltaTime, Space.World);
}
}
}```
post the entire script where that code is inside
Which layer was unaccounted?
default
ok
ok i think everything is working now
and i have to just settle for the fact that unity cant do that many collisions
That's good. Because I don't think I can help you further.
It is - for lack of a better term - wrong, to use transform-based movement with Rigidbody objects.
I'm not sure how it will pan out, but if you say it works, then good stuff.
although one idea is why is a rat that is nowhere near the player checking for a collision
instead of checking all the rats why not just check the ones near the player
Indeed.
Two things:
- Comment your code, or a break from coding might cause you to lose bearings in your scripts.
- Consider rewriting the transform-based movement into Rigidbody-based.
Comments would also help those helping
so rigid body movement is better in every way
No, but it would probably be an optimization.
However, if you are nowhere else using Rigidbody-based movement, it is fine.
I suspect you are currently only using the physics system to identify collisions.
The reason it would be an optimization, is because your transform-based movement is calculated every single visual frame.
And also, if you are to publish the game, it would be best to keep collision-detection to a fixed timestep, so that the game works the same for all framerates.
what makes unity better than tiny game engines?
I am the wrong person to answer that question.
You're welcome.
I was initially only intending to help with a single multi-stepped movement formula 😅
Good luck with your game.
im still a bit confused at why java runs faster than unity if unity is c# and modern
@hollow parrot I mentioned earlier, but if you're interested in finding ways to optimize this further, upload your project to GitHub or other source control system, and I can have a look. While, several thousand objects would definitely be an overkill, what you actually had on screen at that time should be possible without hindering the performance too much.
From what I've seen in the profiler yesterday it looked like the experimental physics jobs were enabled. While usually they'd make things faster by multithreading some of the physics calculations, they might cause more harm in extreme cases and with certain settings. So one thing to try would be to disable the jobs and check the profiler again.
So I ran a test with rigidbodies spawning in a radius and moving towards the center, where they get destroyed on colliding with the cube. With around 6k rigidbodies, the whole physics simulation takes around only 9-10ms which is quite amazing.
Also, it looks a lot more like 6k compared to your screenshot.
Now, I tried enabling Use multithreading(that apparently you have enabled judging from the profiler calls) with the default settings in physics2d - job options and it went down to 6ms at max which is even more amazing.
Which collider type?
Circle.
I know they used capsule, so it should probably be a bit more expensive.
nice
what happens if you increase the collider radius?
also, are they colliding with one-another?
Capsules seem to add 1-2ms more.
Hmm... Radius does seem to increase the processing time
or rather the capsule size
And no, they don't collide with each other.
Interestingly, enabling multithreading takes it back to 6 ms
I believe unity does some optimization with quad trees or something. And there the size of the object could decide how many cells the object is in making it less efficient.
ah
Without multithreading, it stabilized on 12ms with capsules having 4,8 size
But there were spikes that took more time
Sounds scalable 👍
to a point
Also interesting that small colliders would demand less resources.
Something to consider in the early stages of a prototype
Yeah, still it's definitely not made for such a scale. That's why DOTS appeared in the first place
still, 1-3k is good stuff
What happens if you introduce a collidable rigidbody, which simply moves through the sphere?
You don't need to entertain me, but man I'm curious 😅
small colliders vs big/medium rigidbody
like a ship flying through an asteroid field
Are the rigidbodies Discrete or Dynamic?
discrete
Adding a bigger rotating circle that collides and destroys the small red capsules significantly decreases the performance without multithreading. Just the physics update was going up to 100-200 ms. Admittedly, it was also, due to the fixed update snowballing due to fps drop. There were probably 3-4 fixed updates per frame making things worse.
But what amazing is that turning multithreading on, took it down to 6-10 ms
It also seems like the more spaced out the objects are, the better the performance is. So I guess my guess on quad trees is correct.