How do I knock back my player when they shoot? - c#

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)

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

Restrict Player Movement to a Area of the Map Temporarily Unity Game Development C#

When my player gameObject collides with another gameObject, I want to keep the player from leaving a small area around this other gameObject until the player has defeated the enemy. In other words restrict the movement of the play until the enemy has been dealt with. I just don't know how to restrict the movement temporarily to a small amount of space local to where the initial collision took place.
I think possible solutions are to clamp the transform, or to create a another gameObject to physically keep the player in this area using rigidbody and a collider.
My player controlled movement right now is simply Input.GetAxis for both vertical and horizontal input and using transform.translate.
Thanks for reading my question. Any help would be appreciated, even something you might think is obvious, I'm still new to this. Thanks a lot.
Without knowing any details about your game (2D,3D,etc.) my initial thought for doing something like this would be to spawn invisible wall gameObjects around your player and the enemy they collided with. Action games like Devil May Cry do this a lot, except they have pre-defined level spots where the walls are going to spawn.
When the player defeats the enemy, you could delete the walls.

How can I make a gameObject rotate while jumping, then once it collides with another gameObject, return smoothly to its original rotation?

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.

Categories