Unity3d Enemy Pushing MainCharacter Back - unity3d

I am building a simple game where I want to AI enemy to chase and run to my main character and attack it but before I can get to the point where it is attacking I need to figure out this problem.
When my enemy gets to my main character it keeps going forward and keeps pushing my main character back instead of stopping how can I fix this? Both characters have rigidbodies and colliders.
Thanks

The enemy will never be at 0 distance to the player, because the colliders of both player and enemy. It's "impossible" and this means that the enemy will keep on running forward into the player forever.
To fix that, you should set a minimum distance for the enemy AI. This way, the enemy won't move unless the player is out of range.
If you're using Unity's default AI or any other kind of AI from the Asset Store, you will probably find this setting on the script controller. Just look at the inspector and check if you can modify it.

You could try to use "OnCollisionEnter". As far as I know, you could use collider as a trigger of a future action of the enemy and player. Maybe you could set collider in a wider range than that of the characters of yours. Set a specific range that when the player is within that range of the collider it would chase the player and would attack it once it has come to a specific distance then perform the script that you want.

Related

In Unity 2D, how do I achieve Non-trigger collsion that doesn't "physically" move the rigidbodies?

I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine

Setting up a sliding character in the right way

I would like to try and make a game similar to Altos Adventure. I am having trouble starting.
How to set up the terrain so that my character doesnt get stuck?
I simply drew a test sprite for the ground in photoshop. What collider2d should I use?
I use polygon collider and reduce friction in the material. But the character gets stuck, hits small invisible bumbs and it feels awful. The worst part is connecting 2 ground sprites together! The point where they connect is always messy.
I have a question for the future as well. How would I add the endless part of the game?
Would having many "pieces" set up and just spawning them as the player rides down work?
I havent written any code yet as the problem is simply in the physics in my opinion.
Thanks to anyone who takes the time and tries to help!
To move your player use rigid body.AddForce(Vector3 direction, ForceMode forceMode); then create a Physics Material with friction = 0 and put it in your player's collider.

Climb down ladder in 2D platformer game

For my game, the player character requires to climb up and down ladders those are placed in the gameplay area.
At present, I can able to climb up for my player character to climb down at present I don't have anyway. Because platform box collider applied with platform effector, so for the climb up, effector does not create any problem but now after reaching the top, it becomes solid platform so now I can't able to move downside.
For climbing up, I have followed this tutorial: How To Make 2D Ladders In Unity - Easy Tutorial
I am looking to implement some physics so I can reach downside to the ladder after reaching top.
You need 2 boolean variables isClimbingUp and isClimbingDown which depend on pressed key and a second ray, which will check -Vector2.up direction. Then just add one more 'else if' statement for down direction.
Yes, I have managed to solve this, and the game is published in the stores.
You can check using the below link:
Humpty Trumpty's Border Wall
This is my overall physics setup for the stair:
I have applied a trigger collider to my stair object and disable player gravity scale when the player within the stair.
Then after within the trigger enter and exist, I have done this:
Physics2D.IgnoreCollision(m_CapsuleCollider, myStair.platformCollider, false);
Disable collision between player and platform colliders when the player is within the stair.
I don't think, Platform Effect 2D becomes useful to me in this process but I didn't remove this to remain in the safer side.
So you have to keep a reference of the platform object which is attached to Stair.
I hope you will get a general idea to solve this problem.

uNet rigidbody movement latency issue

I am making a Multiplayer FPS using this tutorial by Brackeys and am using a rigidbody for Player movement.
If i move forwards on computer 1, then there is about a half second delay until the player stops on computer 2.
I don't know if this is normal or if it stops when you start using paid servers.
Thanks.
Play around with linear drag and drag on your player's rigidbody and see what fits your needs. Usually 999 linear drag and 1-5 drag does the job for me. Also, you should change physics material on your player's collider according to current state - if he is moving set it to zero friction, and when he is not moving - switch to max friction.
For the player you should use a CharacterController instead.
For that very reason.
The traditional Doom-style first person controls are not physically
realistic. The character runs 90 miles per hour, comes to a halt
immediately and turns on a dime. Because it is so unrealistic, use of
Rigidbodies and physics to create this behavior is impractical and
will feel wrong. The solution is the specialized Character Controller.
It is simply a capsule shaped Collider which can be told to move in
some direction from a script. The Controller will then carry out the
movement but be constrained by collisions. It will slide along walls,
walk up stairs (if they are lower than the Step Offset) and walk on
slopes within the Slope Limit.
The Controller does not react to forces on its own and it does not
automatically push Rigidbodies away.

How to restrict which colliders are triggered?

Sorry if similar questions have already been asked.
I have a character that can hold up a shield to block incoming damage. The character has a circle collider for his body space. The shield has a box collider that blocks off a portion of whatever direction he's facing, and it's only enabled when the player is holding down a button. My enemies have weapons surrounded by triggered box colliders which are enabled when they decide to attack.
So, my problem is that when my character is attacked while shielding, sometimes his body collider is detected and sometimes his shield collider is detected. I can't find any consistency no matter what I try.
[Screenshots] (http://i.imgur.com/VHujbcG.png)
[Code] https://gist.github.com/siketh/2401454977d10ed7699b
I've been struggling with this all day and need another set of eyes. This isn't a serious project so if you need to see anything else I'm happy to post more code or explain more of my design.
It's probably because the weapon intersects with both the character and the shield at the same time. There are great tools in Unity if you want to see if that's the case.
Notice that Unity has "Play", "Pause" and "Play one step" buttons at top of the editor window. You can pause the game with Debug.Break(); instantly at the moment player fires or swings the weapon. Then you can watch what's happening step by step. Select the character, the shield and the weapon so that you can see their colliders in action. This way you can see what collides with what exactly step by step.
Similarly you can see the action in slow motion by adjusting time scale. Something like Time.timeScale = 0.1f;
To prevent this,
You may want to look at continuous collision though I've always failed to use it properly in Unity.
You may want to utilize more physics layers. http://docs.unity3d.com/Manual/LayerBasedCollision.html
You may want to detect collisions by using raycasts.
You can enlarge the shield and reduce the weapon swing speed or bullet speed. So that it's impossible to penetrate through shield.
You can decrease the period of FixedUpdate calculations so weapon movement will be smoother and there will be less penetrations.
http://docs.unity3d.com/Manual/class-TimeManager.html
There is a high probability that you are detecting both weapon-to-shield and weapon-to-player collision in one step. So why not postpone the decision one FixedUpdate later. So that you can check if there was only weapon-to-player collision or both collisions. If weapon-to-shield collision detected in previous update, then you know player hit the shield no matter if weapon passed through the shield or not.
Let's say there is no way you can detect if weapon hits the shield or the player. By using some simple geometry, you can check if the attack is coming from the direction of the shield or not.
Good luck :)