Unity Object bounces even before colliding with a boundary collider - c#

Im making a carrom kind of game where Im using physics for striker movement, the problem is my striker object bounces from the edge of the board even before there was a real collision between my striker object collider and the board edge colliders.
Please check this video where I have highlighted my project setup, is I'm doing anything wrong here?
https://drive.google.com/file/d/1xifD--sXHvQWjLbud-Lpt-qG3GAXu-hS/view?usp=sharing
I tried to fix it in many ways but wasn't able to reduce this offset and without resolving this issue I wasn't able to proceed further, because if there are 2 objects very near to each other and if the striker hits only one of those objects accurately still the nearest another object is also considering it as a collision even though there wasn't a real collision!
Is this a known bug? if not how can I remove this offset and can have accurate collisions that are required for carrom kind of games (where multiple objects are near to other)?

I got the mistake that I have done!
In the editor, every time while going to play mode striker object collision detection mode was set to "Continuous Speculative" and at run time I used to change it to "Discrete" or "Continuous" or "Continuous Dynamic" assuming I will be able to change it at run time but that's not the case, even though editor allows us to change collision detection mode at run time, technically it will only consider the mode which was set initially(i.e while starting the play mode), so If I have to see the behavior of "Continuous Dynamic" then I have to stop the play mode and set collision detection mode to "Continuous Dynamic" and run it again!

Related

How to make realistic shot in Unity

I'm just wondering how to make shots in my game more realistic. I mean there are two main ways in implementing shots. First: throw a small projectile and let it detect collisions. Second: use raycast. But in real world (and apparently I noticed this in PUBG) bullets fly really quick but not immediately. Here is why you should aim near your target if it's far away from you to hit it. Because if you aim right where it is it will move and your bullet will miss the target.
I'm just curious if any of you, guys have a nice solution to this problem. Also I wish to find a way to use raycast not every frame. In such things like detecting if you will actually hit the wall when shooting. If you have any good ideas how to implement spread and recoil for different types of weapons I will be happy to read them.
Blue point is bullet point of frame. and in very frame send a new raycast to check cross any human. like red line in this picture.
and bullet path use unity built-in Physics system to do it.

BuildNavMesh is too slow

NavMeshSurface.BuildNavMesh();
I want to do "Bake" of NavMesh dynamically. My game is like Minecraft.
I move the MOB around the world. Player can place blocks. So, if I do BuildNavMesh() I will be able to "Bake" NavMesh with new blocks.
Blocks is placed by the player. And every time player puts a block, I do BuildNavMesh(). Players can jump on them. Player can also destroy those blocks.
As I mentioned earlier, I do not know what to do other than that.
I can only write the above code.
NavMeshSurface.BuildNavMesh();
The best result I want is to use NavMesh to dynamically change the MOB's range of motion.
My navmesh was also building extremely slow. 2 things that really helped were:
play around with the tile size on the NavMeshSurface component, the smaller the faster it will build but it has some tradeoffs.
remove unnecessary layers from you NavMeshSurface component. And this was really the biggest issue. I had the default layer and ui layers in there which caused a big lag, removing them almost instantly rebuilds the navmesh in runtime!

Unity Multiplayer Cube Auto Move?

I have followed the Unity official Multiplayer Guide below and everything works, except for the fact that when I run two instances on the same computer (1 in build run and the other in the play mode) the characters for some reason auto move in a circle.
I have no idea why this is since I have followed the tutorial exactly, unless I missed something :P I am currently on step 9 (identifying local player) and I stopped there cause my players keep moving in circles.
To Clarify, they aren't spinning in place, they are walking in a circle. Just imagine a person following a dotted circle on the floor, same idea.
This issue only happens when i run two instances (build run mode and play mode in unity). if i try only the play mode in unity, everything works fine.
Has anyone experienced this before?
Unity Multiplayer Tutorial: https://unity3d.com/learn/tutorials/topics/multiplayer-networking/network-manager?playlist=29690
I am on version 2017.2.0f3 <-- maybe this is why? should I update to a different patch?
Thank you in advance
Where I spawn the characters
build and run, player just moves in circles automatically
both build run and play mode, they both again moves in circles automatically
I see a first issue in your code:
PlayerController.cs line 36, you wrote
var bullet = (GameObject)Instantiate(BulletPrefab, BulletSpawn.transform.position, BulletSpawn.transform.rotation);
it should be
var bullet = (GameObject)Instantiate(BulletPrefab, BulletSpawn.position, BulletSpawn.rotation);
Since BulletSpawn is already a Transform. Otherwise bullets might not fire in the gun direction.
I don't have any of the player moving without me pressing keybord key.
Here is a screenshot of 2 build run working good:
I also tried Build run + Unity Editor in game mode, I had no problem.
Maybe the problem comes from your keyboard or the input manager of unity ? Since you are using Input.GetAxis, check this https://docs.unity3d.com/Manual/class-InputManager.html
The issue with your character automatically moving is because you have something plugged into your computer that acts as a controller/joytick. Go into settings for controls and set all joystick to the last joystick #. Make sure you set this for all vertical and horizontal movement. That should do the trick.
For example, if you use a 3D mouse like 3D connexion, it could act as a joystick/controller and auto move your character.

Issue implementing proper collision in XNA Game Studio 4.0

I am building a game in XNA 4.0, where a player moves about a 2 dimensional (vertical perspective) map consisting of blocks. My issue is creating proper collision between the payer and the blocks (basic game physics.) The player moves more than 1px per frame, so .Intersects() just isn't enough, I need physical contact collision that can function in a gravity environment. The current version I currently have is a piece of garbage and only works occasionally.
Basically, all that the collision system needs to do is stop gravity when the player lands on a block, and provide some decent physics when the player hits blocks (movement in that direction ceases). The idea behind my current solution is to move the next Position around until it finds a clear spot, but it doesn't work well. I have an idea why, just have no idea how to do it properly.
I know there must be a better way to do this. What would be the best method of making this kind of collision work properly?
Thanks
This does the job perfectly, you just need to sort through a few syntax errors. http://go.colorize.net/xna/2d_collision_response_xna/

XNA collission detection vs. game speed

I have been coding for some years in C# and have now decided to try out the XNA framework to make games.
It went great until I started to implement collision handling in my very simple game.
I can understand how to do collision detection using BoundingBoxes and BoundingSpheres, but after looking at the refresh rate in my game, it quickly became a concern of mine if the two colliding objects were never detected as colliding.
Let me try to explain with an example:
- If a character is shooting with a gun at another character.
- The bullet is heading straight for the other character.
- The bullet gets rendered just before the character.
- Because of the bullets high velocity it now gets rendered on the other side of the character.
In this scenario the bullet and the character never collides, because they are never rendered in their colliding state.
So how do you make sure to detect a collision in this scenario?
For very fast-moving objects, the regular approach fails in the scenario you described. What you need to check for is whether the bullet has collided with any item in the interval between the two consecutive game ticks. This is called continuous collision detection (this is a rather related SO post).
You can do this by basically casting a ray between the middle of the bullet's binding box in the current position and the middle of the box from the old position, and checking if that collides with any other blocks/spheres. This is a rather fast solution and if your bullets are small enough, it should work fine.
For more precision, you can cast four different so-called collision rays from each corner of the current bullet box, to their corresponding positions from the previous game tick. Note that in the rare event of high-speed items smaller than bullets, this might also fail. In this case you would need a more advanced collision detection system. But this would just be a corner case.
If that extra precision is a must, a free 3D physics library, such as Bullet could represent a solution. It even has bindings for XNA!
I think your problem is in tunneling that matches your description.
I recommend you to use Box2D physics engine for XNA. It's fast, simple and will avoid your any problems with collisions in your game.
Check in this manual on 4th page continuous collision and check how Box2D deal with tunneling effect.

Categories

Resources