How can I make two animations work together in unity? - c#

I'm making a first person shooter and im trying to make a reload animation.
I want to make it look like the hand grabs the mag and pulls it down.
How do I make two animations work together?

I remember having to do that for my wrestling game. What I did was create 2 controllers and 2 animations. Controllers should be the same (have same timing, transition and all that). Then make one animation for example animation of how hand reloads a gun. Now memorize sample rate and in which frame you put keys and what do they do (what do they change). Now start animating how mag is getting pulled BASED on what you have done previously. Like in what frame it should go down and in which frame it should be thrown or new one should be inserted and etc.
Then you will have to enter play mode in order to see them simultaneously. I am making a wrestling game and this method is what I use.
Good luck

https://docs.unity3d.com/Manual/AnimatorOverrideController.html
he Animator Override Controller is a type of asset which allows you to extend an existing Animator Controller
, replacing the specific animations used but otherwise retaining the original’s structure, parameters and logic.
This allows you to create multiple variants of the same basic state machine
, but with each using different sets of animations. For example, your game may have a variety of NPC types living in the world, but each type (goblin, ogre, elf, etc) has their own unique animations for walking, idling, sitting, etc.
By creating one “base” Animator Controller containing the logic for all NPC types, you could then create an override for each type and drop in their respective animation files.

Related

How to instantiate multiple objects in area without intersection? (Unity)

The task is to create a bunch of 3D prefabs with rigidbody and mesh colliders in specific box-shape area, so all of them would not be overlapping each other, after which they should fall to the surface beneath. If I set random position for each object, sometimes (especially if there are a lot of objects to create), some of them are being created too close to other, so they push each other while falling, and everything turns into a mess.
Is there a way to create these objects with at least minimal space between each other, so they could physically interact after they have fallen? Please, take into account, that spawning box is big enough to contain necessary amount of objects, no need in huge amount of them, but still the number of objects could be different each time.
One solution would be to make the box Collider smaller for instance if the object scale is (1,1,1) the collider would be (0.95f, 0.95f, 0.95f) or something similar. Of course only if you generate your cubes in a grid like system. After the first collision try to change the collider size again. If the problem persists try to make the colliders bigger and add a physics material with 0 friction.
Assuming you have big enough area for your objects, the dirtiest but the easiest solution (not meddling with bounding boxes etc.) would be to split the logic into two phases and using triggers to reposition the object randomly.
In the first phase, the generation phase, the object renderers are not active and the colliders are active.
Generate an object.
If the collider triggers an overlap and we are in the generation phase, reposition or respawn the object.
In the second phase, make renderers active and make them fall, disable colliders etc.

Unity Dota style loading screen

I'm creating a 3v3 multiplayer game and I'm stuck on an important part before the gameplay comes in.
I was wondering how you would approach creating a Dota 2 style loading screen for loading characters into the game (Picture below).
Creating the UI isn't the problem. The problem is animating it to look clean while also having it actually load the game (terrain/gameobjects/etc) with a progress bar or something.
I load the level with this
PhotonNetwork.LoadLevel('Game');
Then a gameobject spawns each player with
PhotonNetwork.Instantiate('....');
This works pretty well with nothing in the scene other than a plane to walk on. Now I just need to create the loading screen BEFORE the character loads while actually loading the terrain/objects/etc.
If anyone could point me in the right direction I would really appreciate it.
Thank you
Dota 2 style loading screen example:
I think this is a perfect case to use for Scriptable Objects in Unity.
Because as far as i remember there are around 100 heroes in Dota 2 and only 10 of them will be picked. So the images in this loading screen would change based on player input. Therefore, you should create scriptable objects. Then you can change the image(hero) also modify/add nick names to it in Run time. So to sum up you will have 10(in Dota but 6 is your case) scriptable object in your scene but you will only modify the images and nicknames after player selects the heroes.
Another benefit would be since they are objects you can easily animate them move up and down adjust however you want.
Here is a tutorial from Brackeys that is perfect for your case. Good luck!

Unity3D : Performance with multi-camera rendering with a lot of spheric meshes

I have an Unity 3D scene with several cameras looking at the same object (a huge brain mesh ~100k tri) but not necessary with the same point of view.
In the same 3D scene there is a huge number of spheric plots meshes (from 100 to 30000).
In all the cameras i have to display the brain mesh with a part of the plots meshes.
Depending on the camera view, each plot can have a different size (mesh filter and spheric collider), a different material (opaque or transparent) and can be visible or not.
The spheric collider must have the same size than the mesh.
I set up a shared mesh in common for each spheric mesh.
Their material can be one of the several shared materials i have defined.
Before rendering the scene, for each camera view in the OnPreCull function i have to define which plots are visibles and how they look.
This part can be very costly, i tried several things :
setting gameobject inactive : too costly
setting local scale to vector3(0,0,0) : better but i can see that the rendering is still done in the profiler
setting a total transparent material : same result, but the in the profiler the rendering is now transparent instead of opaque
setting a layer not in the cameras layers masks : huge script cost
I don't kwnow if i can make an efficient culling system with all theses cameras looking at the same point...
I welcome any new ideas.
First issue:
Regarding your specific question with the four dots.
Simply set the renderer.enabled = false, that's all there is to it.
Note however that as I mention in a comment, you would never try to "cull yourself" in Unity (unless I have misunderstood your description).
Second issue:
Regarding the small spheres. I suspect you have very many in the scene. You simply can't do that. In video games (the most difficult of all 3D engineering), you just do this with billboarding. It's how say "grass" is done in a scene. You can achieve this nicely with the particle system in Unity, or other techniques. An implementation is beyond the scope of this answer, but you will have to fully investigate billboarding. Simply it's a small flat image which always faces the camera in the render pass.
Issue 2B:
Note however that sphere colliders are wonderful, and you can use as many as you want. I'm sure this is obvious from base mathematical reasons. Side tip: often folks try to "write their own" thinking it will be faster. It's impossible to outwrite the 100? person-years of spatial culling scientific research in PhysX, and moreover they use the metal, the gpu, so you can't beat it.
Issue three:
Is there a chance you're using a mesh collider somewhere in the project? Never use mesh colliders, at all. (It's extremely confusing they are mentioned or used in Unity; they only have one or two very specific limited uses.)
Issue four:
I'm confused about why you are turning things on and off. I have a guess.
I suspect you are not using more than one "stage"!
There's an amazing trick about video games when you have more than one camera. In fact you have "offscreen" scenes! So you may have players in a dungeon or the like. Off "to the side" you may have an entirely duplicate or triplicate setup of the whole thing running (you could "see it if the camera turned the wrong way") for the other cameras. (In the example you would have different qualities on the dopplegangers, coloring, map-style or whatever the case is.) Sometimes you make a whole double just to run physics calculations or address other problems.
Fascinating extreme example of that sort of thing.
In short in your situation,
You likely need one whole 'stage' of a camera and brain for each of the camera views!
Again this can be http://answers.unity3d.com/answers/299823/view.html but it is indeed the everyday thing. In your overall scene you will see eight happy brains sitting in a row, each with their own camera. In each one you would display whatever items/angle etc are relevant. (Obviously, if certain items are "identical, other than the viewing angle" you could use the "same brain with more than one camera": but I would not do that, best to have one-brain-one-camera for each view.)
I believe that could be the fundamental issue you're having!

Hide GameObjects to speed up game performance

My question is, how could I hide a GameObject when it is not being rendered by the camera, but to unhide it when it comes back into the camera view?
My code is the following:
void Update () {
if (renderer.isVisible) {
objectToHide.renderer.enabled = true;
}
else {
objectToHide.renderer.enabled = false;
}
}
The problem is, the object becomes hidden, so it can't be detected and unhidden.
Any help is appreciated!
(The goal is simply to speed up the FPS and improve game performance)
Your answer is simple. The game only renders the objects in camera sight. You can hide an object or even disable the renderer component but its not used for memory management or better process.
There are standard techniques for making a game run more effient. One of them is LOD or LEVEL OF DETAIL. You have a model with different polycounts and different texture resolution. When its near you use the highest polycound and highest texture resolution for further distance you use lower options.
Another technique is level streaming. If you have a big level and you want to game run on low memory or don't make players wait for long time for level loading, you have to do this. If you search a little you can find lots of information on different engines.
Most recent games have an option to choose foliage detail or environment detail. You can make a layer for every level of detail of your game and for every level of option player made you destroy those objects. Maybe I'm wrong but renderer.enable cant be good because the object still is in game and game just doesn't show it. You have to change many components to make it work and controlling all of them is hard.
To improve performance, you better try to destroy scripts and gameobjects as soon as they are not needed. Like bullets coming out of the game board, scripts that run just once and already finished, ...

Two Classes Dependent On Each Other

I have Camera class, which handles camera behavior. Among it's fields is a reference to the target's Cube class (Cube is just one of the object, but I won't mention others to keep it simple). In order to calculate the View matrix, I need the camera's position and target's position, so I can explain to my program that: "the camera is placed here, and from here it's looking at this cube". Should the cube happen to move around, so too would the camera's point of view change automatically.
So far, everything is good: there is a Camera class which depends on the Cube class, and there's the Cube class which depends on nothing (in this example).
I get to a problem when I need to draw a cube, or anything else -- in order to draw something, among the required values it the View matrix of the Camera; that's the one that I've just calculated in the first paragraph. In essence, this means when I get to the point to draw things on-screen, the Cube class becomes dependant on the Camera class as well, and they're now dependant on each other. That would mean that I either:
need to make the View matrix field of the Camera class static, so I can access it directly from the Cube class.
need to make a method (eg. SetView) in Cube class, which I can then invoke from the Camera class (since I already have it's reference there).
need to keep a View matrix in outside scope.
need to make a bidirectional dependency.
But, I don't like either of these:
there are more cameras which handle multiple views (currently there are 3 of them on-screen) and there may be more (or less).
this makes the code slightly (sometimes, maybe, very) unreadable -- for example when I'm drawing the cube, it's not quite clear where the View matrix came from, you just kinda use it and don't look back.
I would access the outside scope from the camera class, or the outside scope would access the camera, and I wouldn't like this because the outside scope is only used to handle the execution mechanics.
I like to keep my reference fields "readonly" as it's currently everywhere in this system -- the references are set in the constructor, and only used to get data from referenced classes.
And, if I haven't made it clear, let me just repeat that there are multiple Camera objects, and multiple Cube objects; whereas any Camera may or may not depend on any Cube, but usually there is at least one Camera dependent on a Cube.
Any suggestions would be appreciated :)
If your Cube must know how to render itself with respect to a Camera (and therefore must know about the Camera), then it probably doesn't make sense for the Camera to know how to align itself to a Cube. The alignment behavior that's currently in Camera probably belongs in a higher-level class like a CameraDirector that knows about both Cubes and Cameras. This improves class cohesion, as it splits off some of the responsibilities of Camera into a different, tightly-focused CameraDirector class. This lets the Camera focus on its core job and makes it easier to understand and maintain.
Assuming your DrawWorld() routine already knows about Cubes and Cameras, I would pass the view matrix to the Cube's Draw() method:
foreach (Cube cube in cubes) {
cube.Draw(..., mainCamera.ViewMatrix, ...);
}
That way Cube only "depends" on Matrix and not on Camera. Then again, maybe that violates rule 3 above. I can't do much better without seeing some of your code, though.
Two alternate options:
Create base classes for the Camera and Cube that don't link to each other but still contain most of the logic you want to use. Then you can add a BaseCamera reference to Cube, to which you would assign a Camera object, not a BaseCamera object. (And a Camera with a BaseCube field.) This is the power of polymorphism.
Define an ICamera and ICube interface. The Camera class would then use an ICube field to link to the cube and vice versa.
Both solutions will require you to take care when creating and freeing new camera and cube objects, though. My personal preference would be the use of interfaces. Do keep in mind that the ICube and ICamera interfaces should not link to each others. Their related classes will link to the other interface, but not the interfaces.
I made each object class responsible for it's own rendering.
In your case, you would have to pass each rendering method the graphics instance and the viewpoint relative to the object.
A drawing class would have access to the instances of all the object classes, and call the drawing method of each object in whatever order makes sense. Your object classes would probably have to have another method that determines the distance from the viewpoint, so that you can call the drawing methods in a furthest to closest order.
When doing XNA i hav had a sililar problem.
I soolved it by adding an interface to the camera into my Draw method interface.
Its not pretty and a camera gets passed around everywhere, but it worked well.
The real thing to get is that your update and draw loops are seperate.
when drawing you ahve a list of objects to draw and your draw routine has some camera class passed to it.
the alterative to to code in a way to your classes to produce a list of all their objects that require rendering. Pass this to the renderer which contains the camera.
The point being that a list of cameras is maintained, along with a list of drawable objects despite all of these objects also belonging to a logical pattern describing game state.
Read about inversion of control. It is all I am describing really.

Categories