Unity3D Object 'Shoving' Mechanic - c#

I'm trying to make a 2D top-down perspective game in which I want to try and implement a shoving mechanic as a way of the player interacting with the environment or even enemies, but I can't seem to nail down the physics behind it.
(Player object (P) and interactive object (O))
My Question is: what is necessary to make this work properly? I've tried a few things out, such as having the information to add the force to the Rigidbody2D on P first and then on O and I'm trying to get it so that the player pushes the object in the direction it's facing, which I have a script for transform.up to face the mouse. I did also try to implement a dash mechanic to see if it would push an object further, however the physics meant that P would dash in a random direction as opposed to being towards the mouse

give the player object a rigidbody and a colider and the object a colider, should work no problem then!
http://docs.unity3d.com/Manual/class-Rigidbody2D.html
http://docs.unity3d.com/Manual/class-BoxCollider2D.html
should just work automatically then, Unity doing all the physics work for you
edit
if you want the object to be more strongly affected just reduce it's mass int he rigidbody in the inspector, or increase the players mass

Related

Unity Character controller bouncing

I have a game object with a Character controller. I set it up to move and jump. Everything works fine, but when I add an Animator, the object bounces once after landing on some other object that contains the box collider.
I have already tried many ways, but they did not lead to a positive result. I tried to edit the fbx model, change the anchor point, change the position of the Character controller on the object.
When I removed the animator, the problem disappeared, but I need to use movement animation. And for some reason it affects gravity. Without it, the player's game object jumps higher. I've been working in Unity not so long ago and I don't remember how to solve this problem.
If you are using the RigidBody component, you may need to make a physics material for your player. The physics material defines some properties of the object such as how much it bounces. In the Assets folder: click create > physic material
This will create a physics material. Edit the bounciness and add it to the RigidBody of your character.

Distance between game object and ground in Unity

I recently started having a look at game development with Unity and was trying to make a simple 2D character with basic movement abilities. This character is supposed to jump and move from side to side, but only if it is standing on something.
Now my question is: How do you check if a player is standing on something? / Get the distance to the next game object / collider beneath the player game object?
Would greatly apreciate any helpful answers and especially explanations on how exactly it works. Thanks!
To do this, you need to send a ray to detect the point of impact on the ground and then calculate the distance. The code below sends a ray from the center of your object down to the maximum height (3) and gives the size.
public LayerMask groundLayer;
public float maxRayLength = 3;
public void Update()
{
var hit = Physics2D.Raycast(transform.position, Vector3.down, maxRayLength, groundLayer.value);
if (hit) Debug.Log(hit.distance); // it will print current distance from pivot
}
If you want to calculate the height of the ray from the character's foot, there are two methods, one is subtracting half the height of the character from it.
Physics2D.Raycast(transform.position-transform.up*height, ....)
Next one is to use an empty object at the base of the character, which we refer to instead of the center.
public Transform pivot;
Then..
Physics2D.Raycast(pivot, ....)
There are a few ways of actually doing this.
The most usual although a bit complicated way of doing it for a beginner is using Raycasts. A Raycast is basically a small invisible line that starts and ends where you tell it to. If anything of a specific tag or layer is caught in it's crossfire you can basically pull that object from your code. Raycasts are used for a lot of things, most notably in Shooter games to shoot or in games like Skyrim to pickup objects and interact with them.
Another way to do this, which is a bit more popular in 2D games is to create a "feet" GameObject and make it the child of the player in the hierarchy. You can add a box collider on that GameObject and check the "IsTrigger". You can add a Tag to your ground objects and through your code using the OnTriggerEnter() and OnTriggerExit() Methods you can basically tell when your character is floating on air and when he is on ground.
Another popular method is to use the Physics.OverlapBox() Method which is pretty much the same as the Trigger Method but you are creating an invisible box (much like a raycast) and instead of only getting notified when Triggered (something enters or exits) you check if the invisible box is colliding with another object/tag/collider (which could be your ground).
There are also a few different things you can do with a Nav Mesh (mostly in 3D) but I think that for now these 3 should suffice!

Unity 2D games, I have troubles with 2D collliders

I have a lot of problems with some 2D colliders.
In the past, they work, but now they are horrible:))
I already try with Kinematics, OnCollision2DEnter, OnTriggerEnter etc.
The objects are already on the same layer, the same position on the Z-axis, they interact with each other but my game can't detect the collisions.
The rigidbody and collider don't have material but the objects have skins enter image description hereenter image description here
I've realized that the gravity scale of your ground object is above 1. You can make it 0 so it does not get affected by gravity. Gravity Scale is on your ground object's Rigidbody2D component right below angular drag. Although I could be wrong since what kind of collisions you are expecting and your movement code, that should help resolve your problems.

How to detect collisions for a rotating child object in Unity3D C#?

I am making a First Person Shooter and I have a rigged player with a mouse look script, and I used to rotate the spine with spine.transform.rotation, but this method does not look for collisions, this means if I rotate and I am standing near to a GameObject, I glitch/bug through the object. So I tried to do it with a Rigidbody but this doesn't seem to work, I tried: rbSpine.AddTorque(new Vector3(mouseLook.y,0,0), ForceMode.Force);. This doesn't do anything. And I have another problem with the Rigidbody, I've only a rigidbody attached to the spine, my player has multiple simple collision and these overlap eachother a little, and my rigidbody is pushed away into the air slowly. Does anyone know a solution for this?
I put a rigidbody component on the player, this will take care of the collision detection for itself and all its child objects.

Jiggling a gameobject in Unity

How do I make a gameobject like a sphere to jiggle in space, meaning rapidly moving around in random directions while holding it's original position.
I know I can come up with some really long codes to kind of simulate this but I was wondering if there is a trick to it.
Thanks
I think what you want are joints. You can attach a spring joint component to a gameObject with a rigidbody. On the spring joint, set the connected body to a seperate gameObject with a kinematic rigidbody, then play with the spring joint settings until you get a nice jiggle effect. Probably increase the Spring value a bunch.

Categories