Character stuck on a tile unity3d - c#

I have no idea what to say, but I have this image which shows how the character is stuck, the character gets stuck when I try to jump couple times, character is not falling.
if I jump 1 more time, the character jumps and drops to the ground
Here is movement code (didn't want to paste it here cuz it is kind of big)
oh and btw this is 2.5d game, 2d in a 3d engine

Maybe you are stuck on a subpixel?
Check the colliders: Window->Analysis->Physics Debugger.
Check the x-coordinates of the blocks in each "tower".
Change from boxcollider to spherecollider (so you slip off of small edges?)
What about the "GroundCheck Transform"? I don't see why it's a separate transform, is it a child of the player?

Related

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!

How could I implement a pendelum to stabilize my player?

So I want my character to act like a pendulum when he lands on the ground to stabilize himself. (Like this game does it: Basket Random, when a character lands he begins to swing until he comes to a full stop). How can I achieve this kind of pendulum? Im using a Rigidbody on my character, so a Hinge Joint wouldn't work since the hinge is gravity dependent. Unfortunately, I don't have any idea how to implement this so I have no code to show.
So the procedure would look like this:
Player lands on the ground
Player starts swinging
Player swings in the other direction with less momentum
The process repeats itself until the player lost his momentum and stops swinging.
Any help is really appreciated
As #Leo Bartkus (Thanks a lot Leo) pointed out I only had to adjust the center of mass below my character. If you don't know how this works, check out this tutorial: Center of mass tutorial

How to make a character walk(unity 2d)

I'm trying to make my character(a box with 2 long boxes as legs) walk using the legs. The point of the game is the challenge of balancing the character and walking with the legs. I've tried using Hinge Joint 2D but I've never used it before so I have no idea of how to use it. I've also seen people use animations but I'm not sure if it would suit my game well because I want the player to be able to rotate each leg 360 degrees so he could find creative ways of walking. Any suggestions?
Joints seem like the best option here. The official unity youtube channel has some good resources for joints: https://www.youtube.com/c/unity/search?query=joint
this is my first answer so please fell free to comment if you have any doubts.
what you and use is making a script to move the leg or in this sense rotate it in one direction. Something like:
GameObject playerLeg;
Input.GetKeyDown("x");
playerLeg.transform.Rotate(10, 0, 0);
if you don't clamp the Leg rotation you should end up with a 360 rotation.
P.S. don't forget to add colliders to the player Body, legs and Floor.
if you want to go a log way with smoother animation and a bit more complex gameplay further in the game you can check out Procedural animation

How to make object follow waypoints to a center point?

Im not sure how to approach this and looking for suggestions - I have a moving box (moved by the user) with paths cut into it, and a character that traverses the paths. The moving box moves as well as rotates.
At all times, I need the character to follow the paths (NOT walk thru walls) to get back to a point on the box that is
facing the screen (even when the box rotates)
Middle of the screen (even as the box moves up/down)
Here the circle scribble is the character:
What would this be called/any approaches? Is this a waypoint system or something more complex?
A nav mesh would be better if you wish to make a more complex AI for the other character say if you wanted the character to chase the player. If your route is predefined then using waypoints will be the most efficient way of creating what you want in this scenario. Here is a good tutorial by Brackeys of how to implement waypoints.
https://www.youtube.com/watch?v=aFxucZQ_5E4

Video Game Engine Gravity

I was trying to make a game engine that used gravity. I tried making it so your character moves down one pixel (Up in a Winforms)every time a timer goes off, and if it's touching the ground, it goes up until it stop touching it. However, it makes your character bounce up and down continually. Is there any other way I could go about this?
Check if subtracting 1 pixel would put you below ground FIRST, and don't subtract if so.

Categories