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
Related
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!
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
I want to add knockback to my entire player when it shoots, so it can impulse itself.
I'm new to making games, I don't know much about c# and I haven't found any tutorials for the thing I need
Difficult to know what you need help on here. But try to I'll answer the question.
When your player fires their gun, you would typically create a new object and apply a force to it for slow move projectiles or alternatively, you'll do a ray trace to see if the target is hit for laser beam style shots.
To make the player move, use AddForce on their Rigid Body to apply a force in the opposite direction. That direction is simply the negative direction of the projectile's direction (or the ray that was traced)
How can I make a gameObject rotate while jumping "in the air", then once it collides with another gameObject, return smoothly to its original rotation like shown in the following video: https://youtu.be/iOV0Apuwj94
I do not want the cube to abruptly return to its original rotation once it has collided with something. Like in the video, the cube's rotation is just right when it collides (when it hits the ground, it feels natural). I also want the cube to know where the future collision is, so that it can modify speed, rotation etc. depending on the positions of each gameObject (this way, the rotation would be always correct too).
I have tried many times fine tuning the rotation, but I always fail to get it just right (+predicting future collisions is unknown to me). I do not have the experience to accomplish such a task and searching the Web did not help either. I would appreciate any lines of code, guidance or help from the community. Thank you for your answers.
Here is a pseudocode I can think of which might work for you:
while(cube.y> 0)//or greater than the right value of y
{
if(cube.y= 0)//or the right value of y
{//stop performing rotation}
//perform rotation
}
This might just solve all your problems, since you are using a RigidBody for the cube which has a collider and it should automatically align to the ground due to gravity, in my opinion, making it feel even more natural.
I am playing around with Unity3D and adding physics to the objects in my scene. I currently have a sphere (planet) in the center of the screen, and I have another sphere (moon) positioned outside of it that is not moving. When I run the game I want the moon to orbit the planet by applying a force to it. I have been able to get the force added by calling rigidbody.AddForce() and that will move it in the specified direction but it won't orbit. I'm not sure how to add force to it so that it will continuously orbit the sphere at a constant velocity.
I've tried some examples using a ConfigurableJoint and it orbits, but it starts out bouncing a little and then starts the orbit. My goal is to have a bunch of orbiting moons orbiting at their own speed that are able to bounce off eachother but not lose their velocity.
Any ideas?
Generally speaking you will fail, eventually, because rounding errors in your integration method will slowly throw you out of orbit. You can get very close in the ways suggested, but you could consider doing something more like the Kerbal Space Program, which seems to precalculate the orbit as an ellipse and then follow that ellipse until it has a reason to believe it should stop, rather than strictly "simulating" the orbit ...
If a collision occurs, allow normal physics to resolve the collision, and then recalculate your new orbit based on the result and start following that.
For the moon to orbit you would need to give the moon an initial velocity. Then have it accelerate towards the planet, that is a constant force.
gameObject.rigidbody.AddForce(1, 0, 0);
gameObject.constantForce.relativeForce = Vector3(0, 1, 0);