changing friction in c# Unity - c#

I am working on a project in my game programming class, and I want to change the friction of a physics2D material, and I can't find anything about changing it. Do you need to import anything, and what would the code be? thanks!

There are a couple ways you can do this.
From the UI
From the code (in C#)
gameObject.GetComponent(CircleCollider2D).sharedMaterial.friction = 0.0f
Where gameObject is your gameObject
Refs
Physics Material 2d
Box Collider 2d
Shared Material

Related

Manipulating 2D Lighting with editor version 2021.2.19f1 with C#

I'm not sure what I am doing wrong, but I am currently having an issue manipulating the 2D lighting of objects. Everything looks normal in visual studio, no errors, but when I try to initialize the code in Unity, it errors out and says the object doesn't have Light on it. 2d lighting seems to be a script, instead of a different type of object like with 3d lighting, but I can't reference the script. It's very hard to find info on this because it seems like the URP gets updated very regularly, and nothing I've found explains what I need to do to manipulate the settings like intensity.
Does anyone have any advice for me?
First create a new 2D light by going to Light < 2D < Point Light 2D (You can use any Light).
Now you should have a 2D light in your scene. Next attach a script to the light for example LightManipulator.cs.
Inside of LightManipulator.cs you can now reference the 2D light by declaring it as a public variable.
public Light2D light;
You can now change various values of this light. For example setting the intensity to 0.5f.
light.intensity = 0.5f;
EDIT: You have to import this at the top using UnityEngine.Experimental.Rendering.Universal;

Unity3D Rigidbodies acts like objects is elastic and falling inside eachother

I was making a game about stacking objects on top of eachother but when I spawn a new object above it falls inside in other object and behaves like elastic material.
I didn't specify any Physics Material to the Rigidbody, It's just a vanilla Rigidbody component.
Here is a gif about my problem
You might have forgotten to add a Box collider to the cube prefab.
Make sure that the collision detection method on the rigidbody is set to either continuous or continuous dynamic. Make sure there's a collider as well.

Unity C# Adding Material Component to new GameObject

i have a problem about adding Material component to my new GameObject.
Situation: I have a big cube and a bullet. After bullet touches to the big cube. Big cube will be pieced 5x5x5. This is ok. I created small cubes for that. I wanna add to these cubes colors. I know that blabla.AddComponent();
blabla.GetComponent().xxx;
I used this method so as to add Rigidbody, but i couldn't add a Material.
My code:
piece.AddComponent();
piece.GetComponent().color = Color.red;
Can you say how can i add material ?
You can't, because Material is not a Component.
Rather, do the following:
var mr = GetComponent<MeshRenderer>();
mr.material = your material

Dragger with collider on Unity 3D

I´m making a simple CAD application on Unity 3D using C# to configure closets. I'm trying to limit the movement of the shelves so you can move them only on the hole, so I have a dragger attached to every piece of the closet I want to move, but obviously they can cross with each other.
I thought I could use the collider system that Unity has to limit this movement but since I never worked with Unity before I´m kinda lost. This is my dragger so far:
mousePosition = new Vector3(Input.mousePosition.x, cubo_tmp.transform.position.y, distance);
objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
objPosition.Set(objPosition.x, cubo_tmp.transform.position.y, cubo_tmp.transform.position.z);
I keep y and z components so it can move only in one direction.
Box Colliders are best for cuboid shaped objects.
In the editor, if you click GameObject > 3d Object > Cube, Unity will add a cube with a Box Collider to your scene:
If you want to add a Box Collider to a GameObject that doesn't have one, click Add Component in the Inspector panel and type in "Box Collider" to find it. It looks like this:
Note: If the IsTrigger box is checked, the Collider acts as a Trigger rather than a Collider.
A GameObject with a Collider will "collide" with any other GameObject that also has a Collider, with a small exception involving 2 Mesh Colliders that are both set to Convex.
EDIT: I think you are able to intersect the objects because you are modifying Transform.position directly in your code. This might override the collision behaviour.
Also Go to Edit > Project Settings > Physics and make sure your layer collision matrix allows collisions for the layers on which your objects are placed.

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.

Categories

Resources