Is there a way to animate a sprite partially in Unity - c#

I plan on animating my 2D characters in unity
However, their actions are multi-dimensional, so they could, as an example, be walking at the same time as looking up or firing their gun, each animation would target one part of the sprites and if possible be independent.
the sprite sheet would be far too big to present every possible animation combination,
so is there a way to animate parts of a sprite individually?

There is an official Unity Package which supports 2D character rigging and other advanced features such as sprite swapping: https://docs.unity3d.com/Packages/com.unity.2d.animation#2.2/manual/index.html
Be aware that it is still marked as being in preview

You could try creating the parts you want to move independently as separate gameobjects. Then use the buit-in animator to create your aminations or use scripts to transform/translate/rotate parts.

Related

Unity - Detect collision with a specific tile, depending on physics shape

I have my game set up to detect collisions with a TilemapCollider2D. All of my tiles have their own custom physics shapes (half cells, triangles, etc.), and so far this part works.
Now, I want to have the ability to not only detect a collision, but also detect which tile that collision was with. Most solutions I've been able to find involve finding the cell coordinates of the collision, through tileMap.WorldToCell(worldPos), which would work great with square tiles. However, since my physics shapes are VERY irregular (sometimes much larger than a single cell), I need something that can detect based on the physics shape itself which tile was hit.
If this isn't possible with the TilemapCollider2D then I may have to find a solution using multiple colliders. Any idea of the best way to accomplish what I'm looking for? Thanks in advance!

How to add colliders to complex maze

I downloaded a maze sprite for expiremental purpose to use in my game and I'm trying to add colliders to the wall. But since the maze is complex, it is quite a lot of work to add a Box collider 2D to each wall.
I tried using the Polygon collider 2D and it was some sort of inaccurate mesh looking collider.Is there any better way to add colliders to a maze or is it possible to do it programmatically adding colliders by somehow detecting the structure of the maze?
Here is something similar to the maze I'm using:
Add comment
There are a few answers to this question, depending on your approach.
1. Sprite-based Approach (where you have an image of a maze)
Make sure that the sprite is transparent, and only opaque on the wall areas (a PNG image can have transparency). After you do this, then you should be able to attach a polygon collider to automatically create a collider. If the image is too big/complex like you say, then you might want to split it up into several different images (4 quadrants for example), and then arrange them and attach a polygon collider to each object. In general, the simpler a collider is, the more accurate and efficient it is.
The downside to the above approach is that you are having to do a lot of manual work. If you knew that you had a lot of hand-drawn mazes that you would need to build colliders for like this, then it might be worth automating the process described above with a script, but that could get complicated fast unless you know what you're doing. Essentially, the automation script could recursively split the sprite into quadrants, create corresponding GameObjects, and add PolygonColliders to each one.
Manually splitting the image in a photo-editing program or making an algorithm to generate the maze and colliders might be faster for you than automation, depending on how much you want to get into the code.
2. Algorithm-based Approach
Luckily, there are a lot of maze programming tutorials online. Most are for 3D mazes, but the logic is the same to make a 2D maze. If you're interested in this option, then I found tutorials on the topic here, and here. In order to add collision after the maze is generated in each of these tutorials you can add a BoxCollider2D to each side of each cell which has a wall (or if using a prefab, add a BoxCollider2D to the prefab).

What is the difference in scripts in unity between the Animation and Animator?

In all the scripts i used so far when changing in the script the walk speed or adding animation clip it didn't effect it at all.
To change the character speed i need to change it in the Third Person Character (Script) > Move Speed Multiplier.
And to change animation or to add animation i need to go to the Animator window and add a new state and in the state to use the HumandoidWalk then set the state as default or in the script to use this with Play like Play("Walk")
Then what all the properties in the scripts why the speed and animation and others never effect it ? (Not talking about Nav Mesh Agent or Character Transform if needed).
For example i have a script that can accept Walk anim and then i select the HumandoidWalk but that will make the character not walking at all. Only if i make the state in the animator window then it will walk.
It's not only one specific script but in others too.
I see in many places users use Animation or _animation with Play("Walk") and i to make the player move and use animation i need to use Animator or _animator.
Then what is the difference in scripts in unity between the Animation and Animator windows ? What should i use to make the character in this case ThirdPersonController to walk with animation and not just move ?
For exmaple when using waypoints i want when running the game the enemies to start walking/patrolling atuaomtic so i make new state in animator waindow with HumanoidWalk and then in the script specific for a enemy i use the Play("Walk").
Complexity and backwards compatibility.
Basically, when Unity was just created as a product, it was a pretty basic game engine, and a lot of systems that were necessary for game development were not very advanced. Then, a need for something with more capabilities arisen, and in a lot of cases, Unity decided to create a completely new system from scratch, and leave the old one as well.
Now we have legacy and new systems for GUI, for animation, for input handling, for particles and probably for something else I'm forgetting right now. However, it doesn't mean that old systems are completely useless: quite often, you want to use a simple and straightforward system without all the bells and whistles.
New animation system allows you to create great characters, but it also takes a lot of time to learn and set up. If you have a simple animated mesh that just needs to do the same animation on loop, I would use the old system; if I had a complicated character with several layers of different behaviours and animations created to blend with one another, I would use the new animator.
By the way, the same holds for UI: while the old system is pretty bad for working on player-looking UI, it's still widely used for quick prototypes and all kinds of debug menus.

Unity3D Changing Ball Direction When Hitting a Cube

I'm working on a simple Paddle Game project nowadays. In the begining, everything's looking good. But, when I complete my level design and publish my game, I see gameobjects in my scene are moving so slow. I think, I pass over the Unity3D's physics limit. If I try some Math instead of Unity3D's Colider, I can finish my first project. (I tried to use Separating Axis Theorem, but I can't handle x and y coordinates on Unity3D.)
I need your help. Thanks a lot for your time. (And If I can handle this problem, I will share my project on the internet, for beginner people like me.)
In my project, I achieved this simulation with using BoxColider, but because of Unity3D physics limit, I don't want to use colider in my project.
You can't not detect collision. I'd recommend using Unity's colliders for that, since they're really not bad. The first thing I notice is that in your first simulation, you have 4 box colliders, when instead you should just have one. Use the OnCollisionEnter event (on the spheres) to reflect them off your box.

Unity3d blender fbx file gun is apart

hello i exporting a blender.fbx file into my assets folder in unity and it was working perfectly and i positioned it in front of the camera but when i play it the gun looks like its been taken apart whats going on?!if this helps i separated some parts so i could use it for animation cause it looks like all the animation parts are separate is this a unity thing or a blender thing?
http://www.flickr.com/photos/87198010#N07/7985274177/in/photostream
Two options:
It could be that your animation is incorrect.. remove animation to test..
The position of the child objects, barrel and trigger, may be incorrect in relationship to the parent.. I don't know for a fact, just a few thinks that come to mind.
Let us know when you find out.
When you load a mesh into unity, unity will as a standard add an animation module to it, even if you don't have any animations yet. this may be what is making the gun bug.
If there is an animation component on it, remove said component and try again.
Also it might be that different parts of the gun are at different scales or rotations, before you export from blender click and click 'apply rotation and scale', and then you export.

Categories

Resources