How can I make multiple meshes in unity by script? - c#

I made a water swirl and surface mesh in unity by script, and now I need to add wall which is a simple mesh. But I don't know how to add another mesh though. I wanted to find something like meshfilter.addmesh, but there is no such methods though. I 'm wondering how can I add another mesh or mesh filter to add that wall. I use C# btw.

From the description of your problem (which is not clear enough) it looks like you should create another game object with MeshFilter and MeshRenderer components (you can do it either by hands or by script) for your wall.
If for some reason you REALLY want to have several meshes in one MeshFilter then your only option is to use submeshes (see Mesh.subMeshCount, Mesh.SetIndices, Mesh.SetTriangles, Renderer.materials and maybe Mesh.CombineMeshes).

Related

Differences between instantiate as GameObject and instantiate as Transform

So I'm following video tutorials on Unity and the first teacher, that I had for the 2D part, always instantiate the bullets as GameObject while the second one, on the 3D part, always instantiate the bullets as Transform.
And the result seems to be the same but what are the differences? Is it just personal preferences?
You can consider the GameObject as a place holder of different components and Transform is a specific component.
In the image below GameObject is the cube itself and in that cube we have attached different components like Transform, Cube(Mesh Filter) Mesh Renderer and Box Collider
So now in your case you can Instantiate by referencing the GameObject as a whole or its Transform component. Usually because people will possibly modify the Transform of the GameObject they will shorten the path a bit and directly Instantiate it by referring to its Transform. However it will be the same if you would Instantiate it as a GameObject only in that case if you want to modify the Transform of the object you would need to firstly access the component then modify it.
There's not a significant difference, it's partially personal preference, and partially convenience if you are going to be referring to one more than the other.
It can mean less typing to use Transform if you'll be referring to the transform more, or use GameObject if you'll be referring to the gameobject more.

Shake UI GameObject?

How do I go about shaking UI GameObject in Unity? As in, for it to go up and down and side to side.
I have UI Text GameObject that display points. When triggered, I want it to shake. I've tried using Vector 3 but I feel like I'm using it incorrectly. Some help or pointers would be appreciated.
Once again, it's an UI object that I want to shake on canvas.
Thanks!
You can do this a bunch of ways. Attach a script to the ui element. In the script update loop you can play with the gameObject.transform.position values. You could also use lerp to lerp back and forth to different destinations. You can do it in more ways as well but these would be the easiest to implement.
https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

GameObject shaped like a piece of cake in Unity 5?

sorry for my bad english. I'm from germany^^
My question:
Is it possible to transform an empty game object into a shape that Looks
like a Piece of cake? I have a round terrain and I want to "cut" it in
three pieces of cake, because I want to play different music in all three areas. I Need These pieces of cake as a box/mesh collider or something like this, so I can Play different Songs if the Player collides with the box/mesh collider.
Ideas anyone? Please help me. :)
Is it possible to build an object in Blender for example and use it
as an invisible box collider?
Create the shape in Blender. Create a MESH COLLIDER. Assign your piece of cake model to the MESH parameter of the MESH collider. Scale it. Position it. set it as trigger and VOILA!
This would be the mesh you create in blender (RED)
then,
void OnTriggerEnter(Collider other) {
//play song
}
The other answers have covered options of generating a mesh outside of Unity and using it to make a mesh collider trigger for your music changes.
Other options:
Native Unity collider option:
Use two native cube mesh colliders to create a sandwich of two thin colliders at the borders where the music needs to change. Do some OnTriggerEnter/OnTriggerExit scripting to make the music change correctly when the player crosses the borders.
Pure scripting option:
Use the player position relative to the "center of the cake" point to calculate when to change the music based on where the player is located.

Merge colliders of different gameobjects

I'm trying to create an auto spawned of game object out of predefined prefab.
The prefab contains a polygon collider.
The spawned instantiates the new object right at the end of the previous one, in a way that it looks like a single piece.
I'm trying to understand how to merge both of their colliders to be like a single polygon collider defined on the merged object.
Thank you for helping.
Btw if there's a code involved (which I'm sure there is), I would be glad if you could write it in c#.
Thank you!!
So from reading your Q and information provided i would say that you want one polygon collider to be exactly on the other. If this is your goal, when they collide, you can us the transform function and move it to the exact position of the other.

Unity - How do I apply an animation from one game object to another rigged game object?

I am thinking of building a game like the sims and was thinking of how I could make the characters interact with any object without having to systematically change the character's code and prefab whenever I decide to add new furniture and props to the library. I have had an idea that consists of having my animations attached to the furniture itself rather than having it attached to the character (which would require a lot of memory and extra code if we had a lot of furniture in the game library!). I was thinking about it and had in mind that I could add a rig skeleton in Blender that has the desired animation(s) but not rig any specific object - so that I only have the animation, the location, rotation and scale of the bones! and since the characters' rigs are the same as the skeleton's rig in the furniture's animation, they could be compatible!
I hope this makes sense...
The thing is that I realise at this stage that I do not know the Animation class very well, and I am not sure if / how I can use the animation attached to the furniture to make my character interact with it...
Can Animation.Play() take multiple arguments apart from the animation to play and the play mode? Is it possible to specify which object has to play the animation? I am talking about doing all of this from the script - I write in C# by the way... and I might attach the script to the camera so I hope the script does not have to be necessarily attached to the character prefab in order to play the animation... Though I don't mind creating a function in the character's script if necessary!
I am not new to Unity but I have mostly been using cars and character controllers, so I haven't dealt with much animation coding yet...
I thank you in advance for your help and apologise for my weird english - I live in France! ^^
Mecanim performs retargeting of humanoid animations, so for what concern that specific case, you should be able to use the same rig and set of animation clip on different models.
For what concern grabbing furniture, you don't need the object to be rigged. You can use IK to precise move your character's hand toward the target object and then parent it to the relative joint in the hierarchy.

Categories

Resources