C# - Move objects in console application with different speed - c#

I am studying C# at the moment and I had an exam recently. I had to create a console game that has moving objects. I pretty much managed to do everything that was required, except I couldn't think out a way how to make each game object to move at different speed (actually there are only two moving objects, floors and the character), one of the tasks was to make powerups that increase either the character speed or the floors speed.... I couldn't think out a way how to modify their speed separately.. I am currently trying to finish the game but I cna't get my thoughts around this, my "endgame" is to have two variables charSpeed and floorSpeed... Could anyone explain to me how I can achieve this?
Thanks!
My code - http://pastebin.com/TkPd37xD - it's currently a mess, I just want to figure out what is the logic behind what I want to do. A "general solution", here I have only 2 objects, what if I want to change the speed of 10 objects?
P.S. No Classes, I can go up to Structs, I have not worked with Classes yet.
P.S.S. I take any kind of advices or criticizm about my code, so anything is appreciated since I am still learning, but my main concern at the moment is how to solve the problem at hand.

I would use a Timer for each different levep speed.
The callback of the timer would execute the logic of the movement.
This will certainly required a refactoring of your code ;)

Related

Is there an elegant way to populate an area with RigidBody objects?

I'm creating a penny pusher (like the game machines you find in arcades) game in Unity in my spare time and obviously that means I need to populate the shelf with coins so the player's spawned coins actually have function.
My issue is I haven't worked out how to spawn that many coins in an elegant way which won't either slow the game down with spawning so many objects at once or result in a lumped pile of coins rather than the neat spread I need.
I even tried speeding up the simulation by several times and dropping a pile of coins on the pushing shelf so they would "settle" before the player input was enabled. It didn't work.
So I ask if there's a good way to do this as part of the initialisation of the game and any Unity or C# function which would have the intended effect.
Thanks in advance.
If I understand your request correctly it really does dependent. The problem is that your game will be lagging when you have to many physic interactions. So even if you spawned them without alot of physic interactions, once u move one coin it will start lagging.
I would look for an alternative way then physics to handle them. Maybe you can think of a more simple calculation you do your self where you have less interactions going on each frame.
That beeing said a way to spawn the coins would be to calculate a random position at a fixed height (max height) and then find out what the lowest point is where the coin can sit on (so it does not share space with another coin). Now place it there. Even better would be to calculate if it could actually be placed there without instanly falling.
I think the best way would be having predefined "Coin Towers" (prefabs of them) where you have like 10 variations and then place them at different positions but do not allow to spawn any "Coin Towers" on another one. This would result in less physic interactions when spawned too.
May be helpful too: https://www.youtube.com/watch?v=pTz3LMQpvfA

Are My Unity 2D Transform.Position Units Changing Between Builds?

I've put together a little Git repo to share with you some scripts from the Unity 2D platformer I've been working on. I have written some basic WASD movement control in the Movement.cs script (under Scripts/Character in the repo) and it is working really well. It does everything I need it to do, and while it is basic, I intend to polish it up a bit over the course of the development of the game. However, I've been noticing that every time I build the game, every moving entity moves at a faster speed. The "AI" (yes I use the term very loosely) that I programmed into enemies like the Chompas and BadBirds seems to be far too fast or far too slow, as are user-controlled movements and animated powerup bubbles.
Now, I believe I've traced it back to the way I create translations; I add vectors to Transform.Position whenever I need to move an object or entity. These vectors accept float values as their parameters, and seeing as I'm not entirely clear on what those values represent, I feel that may be where the issue lies. Are these distance values representative of some dynamic system of measurement that might be changing between builds? If so, how might I standardize my distances? Am I totally off the mark? Lol. Any and all help is greatly appreciated.
To be perfectly clear, this issue occurs every time I hit play, regardless of whether or not changes have been made. Thanks again!
Git repo
Try multiplying your movement by Time.deltaTime.
If you don't do this (which it doesn't look like you are), then all your GameObjects will move at inconsistent speeds. The reason for this is that void Update() get's called once every frame, and the amount of frames that get drawn every second is variable (sometimes your game will run a little faster than at other times), and so your speed will be variable if you don't multiply it by Time.deltaTime (Time.deltaTime is the amount of time that has passed since the last frame, see the documentation)
Another option is to use Unity's built in physics system with FixedUpdate(), which get's called at a fixed rate.
I would strongly recommend to follow online tutorials for how to make a platformer controller etc., such as this one. Doing this will help teach you good practices and how to avoid common errors.

Unity big scale world simulation performance problem.

We are working on simulation game. We have about 25000 objects at world. All has 1 unity c# script. If we activate empty update function we get 15 fps if we activate empty fixed update with 0.02 time scale we get 1-2 fps at average spec computer. In Will we need to do something on update function. So need some help for this performance problem.
In that case, what can we do for performance?
Thanks for advices and help.
Well even though your question is broad i will try to give some tips. My guess is you are doing some heavy calculations in your Update and this causes fps problems. Also rendering 25000 object at the same time would cause fps issues. First i suggest using Unity Profiler and Unity statistics to find out what is causing the issue. Then my suggestions would be based on the problem i assume:
Use Coroutines instead of doing all the calculations in Update.
Use Occlusion culling for rendering as minimum objects as possible. You do not need to render objects which are not in Camera frustum.
If you have meshes which are detailed use level of detail if you have the chance.
If you narrow down your problem i am happy to help further. Good luck!
The very need to make a call to Update() is one of the major slowdowns in Unity. That's why they are now pusing towards Entity Component System where there is no need for such jumps.
If all the objects are having the same script, it should be quite easy to modify the code so that only 1 object has the script, but operates (i.e. via a for (;;) loop) on all other objects. This should bring massive improvements already.
Aslo if theres any chance your objects share meshes - do use Instanced Mesh Rendering it is faster by two orders of magnitude

Implementing State Machine for Unity3D NPC characters

I can't seem to wrap my head around this topic and i need a bit of a push forward, maybe an example.
I'm currently developing a project including simulating a large number of automated characters trying to fulfill their "needs".
As for now the characters have their need levels stored in their scripts as simple floating point <0,1> value and check every frame if it exceeds a given value and then try to move to a defined point and satisfy their need.
The problem is one character can have many needs and that leads to a situation when an character moves to satisfy one need and it drops just below the threshold it moves to satisfy the next need. Assuming that later i would like the needs to "rise" over time this will be a big problem.
I think i should implement an state machine to transition from idle -> move to satisfy -> wait to satisfy -> idle but as i said i cant quite understand the whole state machine thing. The closest to understanding for me was this: https://github.com/nblumhardt/stateless but I still cant wrap my mind around it.
Would be grateful for any help, tutorials, examples, anything.

How to find a solution for my Gin problem?

Recently I was playing a game of Gin with my grandmother. We played a whole afternoon and as far as I can remember, I didn't won a single game.
So I told here that it with the help of computers it could become a much better player. She couldn't believe how computers could be useful there and that's why I want to demonstrate it.
I already implemented part of the logic, but now I have the problem that my solver is really not so sexy because he mainly is based on a brute force method. That is I calculated all the possibilities, score them according to the chances for a win and choose the best one. Is there any more sophisticated approach?
I'm talking about standard Gin. The implementation is done in C#.
I'm not 100% familiar with gin, but one thing to keep in mind is each strategy can be broken.
When you play, you have to play multiple players. Do they both have the same strategy? Do they have different strategy? If we played you might beat me in gin, but I might beat your grandma. How does your grandma know how to play? If you were given the same hand she does, how would she play it differently than yours? Yes you can take a look at the statistics, but your grandma doesn't play by statistics - she plays by experience. If you want to make it more sophisticated ask yourself "How can I factor experience into the hand?"

Categories