How to deal with Time.deltaTime to reproduce equal simulations in multiple machines - unity3d

I'm doing a simulation online game where there are some balls that move over a table. The player a, in his turn, makes a move that creates a movement in some of the balls. I want player b to see exactly what a did.
I built the physics of the game from scratch, but I'm using Time.deltaTime in the calculations. I guess this is the problem. As the frame time differ from a's machine to b's machine, the simulation will take different values for Time.deltaTime from both machines every frame and won't be equally reproduced on them.
My question is: how can I overcome this problem? Should I just set a constant value to replace Time.deltaTime? Is there any built-in way to do that?
The main mission I want to accomplish is to have correctness between simulations, want the balls to have the exact same positions in both machines. The game time scale in both machines is important too, but not as fundamental as the results.

Related

How to change game speed in mid development without touching the Timescale in Unity (2D)?

I have a game similar to Sand balls and I was wondering how can I speed up the game a bit without touching the timescale? A nice and somehow correct approach would be to scale down all my objects so gravity can also "affect" the drop movement. I already tested that and works as expected, except I have to apply that for 100+ levels... and would break some prefabs (a mass level editor to scale by bounding box would work but that's another story)
On the other hand, I have timescale but feels like it's the incorrect approach since it also affects animations and leads to unwanted behaviors.
So... do you know any other ways to speed up the game?
If objects is falling down, you can increase gravity. Or without touching timescale, you can just create a public speed multiplier variable and set it to 1 at start.
If you only move balls (Assumed from give sand balls reference), just multiply speed variable of ball with public speed multiplier variable. When you want to change the speed, just change the variable. As an example:
var ballSpeed = baseBallSpeed * speedMultiplier;

How do I make a SKSpriteNode avoid another SKSpriteNode?

I am working on an AI mode for a SpriteKit game I am making in Xcode. I have dynamic physics bodies which can be launched towards the AI. I want to make it so that when one of these bodies is within a certain radius of the AI, the AI tries to dodge it. I have started working through different routes and got decent results.
1st method (pseudocode): Check AI.position.x and AI.position.y and compare to incomingSprite.position.x and incomingSprite.position.y. Break this comparison into quadrants if both x and y are within radius. (i.e. incomingSprite is bottom left relative to AI, bottom right, etc). Based on this info move the AI's position to move away from it.
cons:
-lots of code and calculations in update function to account for multiple incoming sprites
-depending on dx and dy of incoming sprite, the AI often make illogical decision
2nd method (pseudocode): Calculate distance from AI to incomingSprite. Then check dx and dy of incomingSprite and set dx and dy to go in reverse direction of incomingSprite. This seems logical. I am a little rusty on reversing vectors to do this. I feel like this might be possibly a good idea though.
Is there a better method to accomplish this? Perhaps a force field on the magnet I could use to repel the AI at a certain strength so that if the incomingSprite is fast enough it will override and still collide anyway? Im concerned about memory as most of the AI logic is in the update function, which can cause big problems if I'm not careful.
Edit: I decided to use the electric field and gave the incoming sprites a positive charge so that they are repelled by the AI. This makes things accurate/lightweight and more interesting. If the incoming sprites are fast enough, they will break through the electric field and still collide with AI. This also gives room as an additional difficulty parameter in my game (the stronger the field, the harder it is to get the incoming sprites to collide with the AI).

SpriteKit simulation speed / applyImpluse speed

I'm in the process of learning sprite kit and decided a catapult style game would be a good project to start with.
I am launching a projectile by using physicsBody?.applyImpulse(CGVector(dx: strength * dx, dy: strength * dy)) and this all works fine.
I'm currently trying to predict the trajectory of the projectile. I am doing this by applying the same impulse to a hidden SKNode and plotting the path it takes.
The problem is that the user has to wait until the impulse/physics has finished until they can see the full path. (see image below)
Path Image
(from the image above) I want the green path to either appear instantly or appear a lot faster.
I've tried to adjust the speed of the SKNode but it doesn't seem to have any effect on the simulation (it always runs at the same speed). I've also tried using SKAction.applyImpulse with the speed property being adjusted but it didn't have any effect either.
The only thing that has worked is setting physicsWorld.speed but I don't want to change the speed of the whole physics world, only the hidden trajectory node.
I was wondering if there was a way to run the physics simulation on a specific node instantly, or at least speed it up so it runs quicker?
Thanks in advance.
You can massively increase the gravity, and then factor this increase into the increase in the force used to project your object. This will result in the same arc of flight, but rising and falling much faster.

AI Awareness with Unity3D

I am currently developing an AI system in Unity3D and am wondering if anyone could suggest which of the following awareness models are better?
Use Physics.OverlapSphere at a constant rate of about every 2 seconds to check a range for anything of interest.
Use a Sphere Trigger Collider that is attached to the AI, when an object enters this trigger it starts getting monitored.
Mostly worried about performance vs quality, I have a feeling that model 1 is faster until the AI needs to check everything around it for a particular item, in which case model 2 would return quicker as it already has the collection of local interests. However, will model 2 take up more resources as a trigger collider needs to be sending out checks every physics update?
Your 2nd approach is best way to do it, even though,i can easily say that it all depends on your requirements and both have their own uses. (I am "only absolute way of doing things kinda guy")
With Trigger
void OnTriggerEnter(Collider other)
you can only get one collision body out of many that have collided. It is faster if you have large collisions and u can definetly expand and contract the radius of your trigger to almost use it as the same effect as overlapping sphere (but not recommended for guaranteed results). As it detects only one collided body its not processor intensive.
With Overlap sphere approach you can
Collider[] OverlapSphere(Vector3 position, float radius, int layerMask, QueryTriggerInteraction queryTriggerInteraction);
you can get collider[] or multiple collision bodies that have collided. This is processor intensive, if you have 50 objects colliding and performance will drop the time you call upon this function.
Another Approach.
For simplicity and proper design of mechanics, Trigger bodies can solve for any complicated scenario. I must advice you, from my experience it is always the perspective you rely upon to solve your problem is the factor that will limit your thoughts and ultimately your results.
Keeping that in mind, I would like you try this approach...create a method that uses
public static float Distance(Vector3 a, Vector3 b);
to give you a boolean answer to detect your actor. It is a very good approach since only a language primitive is streamed (between 2 actors) and not an object and performance of in-built function is just negligible. You can have each enemy actor register themselves (their reference) in the List (to get all actors of interest) inside main actor actor if they are at desired proximity (so yes your enemy checks whether its close than the main actor and tells the actor that he is the one who is very close).
I have tested this approach with 20 other actors having their own scripts running in ipod gen 5, with almost no performance drop.

Half scenes with static elements, other with spawn

I have 2D game, where half scene with spawn enemies and (for example) other half scene, where I want use static enemies and other elements.
I thought to create sript that after some time (for example 10 seconds), will stop spawn scripts , and run the movement of other elements.
So. Maybe there is a reasonable solution to this problem.
[UPDATE]
I need the most sensible solution of such a problem, I do not mean to do this, but how to make it better.
1) Can make static elements, which will be a certain time, just stand behind the camera, and then move ... or programmatically create static elements, over time, in advance of known locations...Or download the entire stack of elements over time.
2) Or can completely abandon this idea. A striking example is the Subway Surf, there static scenes (layout) are created in random order.
P.s. I hope I have explained my problem
Just learn to use "Invoke", it's extremely simple.
Invoke( "YourOtherRoutine", 10f );
So after ten seconds it will run the other routine. That routine could easily stop one script running, start another script running, or, whatever it is you want to do. There are tens of thousands of examples of Invoke() and InvokeRepeating() on the usual Unity forums, etc.
From your reference to Subway Surf, I assume that you want to generate static elements like the path and static trains in subway surf and non-Static elements like some moving trains. If so then I have a possible solution.
You can create pre-defined sets of elements (let say 20 or 30 set with different combination of elements) and then spawn them randomly one after another. e.g. have a look at the two reference images below.
Now, note that you might see these scenes exactly as in the images multiple times while playing the game, this is because they are pre-created, The developers behind Subway surf have created these paths and saved them as prefabs and then spawn them at different locations during game play.
You might have noticed that sometime the path is the same but the position of trains is different. This can be achieved by further creating spawn points on your path and then at runtime randomly select points on which you want to spawn your static elements.
In many cases when there are more than one gates you can pass through (I am referring to the gate in the second image). the moving trains spawns on the path of the gate you cross. Spawning the moving train can be achieved as mentioned in step two with a movement script attached to it. Regarding the question of how to know on what path to spawn there are two possible ways (that I can think of right know).
You can keep track of your players current lane and then spawn the train on that lane.
You can place separate triggers on each lane and then detect which lane trigger was triggered and then spawn the train on that lane.
For other moving trains just use the method in step 2 to spawn them but with a movement script attached.