Unity OnTriggerEnter is not being called - c#

I have a flying Dagger with a trigger surrounding it so when it hits an enemy or wall it returns. However sometimes the Dagger just flies straight through. I suspect that happens because its velocity is too high(200), is there a way to fix that?

This is, almost for sure, caused by the high velocity, and in fact this problem is known as "bullet through paper". There are some tricks to address this issue.
As a start, if you are using a Rigidbody, you can go for the dynamic collision detection: this reduces the performances, but it helps a lot for this problem.
You can use Raycasting rather than Collisions, in order to check for the bullet impact.
A trivial advice: in case of the walls, you could just extend the collider in the direction for which the scene does not take place anymore: this way, you would have "wider" walls, but outside of the game field.

Related

How to move enemy characters?

I have random enemy characters spawned into a map using Unity 3D. The enemies have rigid bodies with gravity enabled. I use MovePosition to move them around the map (in the 'forward' direction) in the Update() method and select a random rotation so they 'wander' around randomly.
Because gravity is enabled when they walk up or down hills on the island, it all works correctly (they stick to the terrain).
The problem arises when I fire an arrow at them. Sometimes they start floating upwards into the sky, despite gravity being turned on.
I tried using AddForce() instead, after reading that may be more appropriate for non-kinematic rigidbodies, but movement became very erratic and so went back to movePosition.
Any ideas how I can stop them floating upwards when hit by arrows sometimes? I'm guessing having enemies walk around randomly and then be able to be hit by arrows must be a fairly common requirement for game designers, but not been able to find a best practice method to use anywhere on google?
void Update() {
...
rigidBody.MovePosition(rigidBody.position + movementDirection * 0.4f * Time.deltaTime);
...
}
It turned out it was due to internal inertia on the enemy objects - just found this video during lunch. Another unityism... so frustrating, but hopefully this answer helps out others in the future:
https://www.youtube.com/watch?v=maIAiSRBEdE
It turned out Fixed constraints were being ignored by unity because of the internal inertia being set on the objects rigid body.

How can i make a rigidbody speed up everytime it collides with a wall at an angle?

I am trying to make a game where i got a ball with an arrow rotating around it. The ball starts as immovable, but when i hit a button i launch it to the direction the arrow is pointing at. Player will have the option mid air to press another button to make the ball "sticky" so when it hits a wall it will stick to the surface and repeat the process till he gets to the top of the level.
Aside from that, i want to give the option to the player to not stick to the wall if he doesn't press that button and instead bounce off the wall but when he does that, the player should speed up with every bounce, giving the option to either play it safe and slow or try to go fast getting more points as he does.
For the early prototyping i used force to move the player up every time he launches but i am not sure how i can make him speed up every time he bounces off the wall. It feels to me like a math problem more than it is a coding challenge. What i am thinking is that i have to find the angle on which player hits the wall, and add force according to that towards the direction he is supposed to follow after the collision.
Sadly i am not that good with trigonometry (working on it though). I am thinking that i might need to use a formula containing sin, cos, tan formulas but i m not sure how to do it. Any help is much appriciated! If you need more information on it please tell me and i ll be happy to provide.
Edit: After the first reply to this question i also found out those links that dive deeper into the subject. I m gona link them here for people that have the same issue.
Bouncing a ball off a wall with arbitrary angle?
http://www.3dkingdoms.com/weekly/weekly.php?a=2
If your ball has velocity vector V=(vx,vy), then after bouncing from standing surface with normal N, ball's new velocity is
V' = V - 2 * N * (V.dot.N)
where dot is scalar product of vectors (vx*nx+vy*ny)
Particular cases:
bouncing from vertical wall (N=(+-1, 0)) causes reversing of vx-component, vy component remains the same. V' = (-vx, vy)
bouncing from horizontal wall (N=(0, +-1)) causes reversing of vy-component, vx component remains the same. V' = (vx, -vy)
Note I recommend to work in velocity vector components, and use angles only when they are really needed.
If you need to calculate bouncing from moving bat, it is worth to change stationary coordinate system to moving one, connected with the bat, find reflection in that system, and revert to stationary system.
Ok so problem was solved!
I want to thank both #MBo and #shingo for their contribution to the solution. While the answer of Mbo solved the problem via trigonometry and gave me nice material to study and figure out how things work, i followed shingo's advice in the comments of my question and managed to do it without diving deep into math.
So basically what he said, and what i did, was to use Unity's physics engine and let the ball hit the wall. After the ball bounces off the wall, it gets a new Velocity vector towards the direction it would go based on physics. I then created an OnColllisionExit check, and when the ball stops colliding with the wall, i AddForce to it to the direction of its new Velocity Vector3.
Works like a charm!
Thank you all for your contributions!

How do you reduce the drag experienced when jumping in unity?

I've created a player character in Unity and jumping works like it should.
But when she starts to fall back down to the ground, I want her to fall quickly.
She is almost floating back down, which interrupts the level's feel.
What parameter am I missing that allows her to fall back to the ground?
I'm going for a Mario feel, where he crests at the jump and then falls fairly quickly back down to the ground.
Simply add more gravity to whatever component that controls your characters' physics e.g. If you are using a rigidbody component then increase the gravity property.

Unity: objects with rigid bodies and colliders going through each other?

Alright, so Im having an odd issue in Unity and cant find the answer there.I have 2 objects, one is movable by the player and the other is spawned. I need the player do be able to move (only along the z axis, meaning forwards/backwards/etc) this spawned object when he bumps into it, but only a little bit.
To accomplish this, Ive attached gravity rigid bodies to both and box colliders to both. Ive increased the mass and drag on the spawned object to make it hard to move here and frozen its rotation/some position to prevent flipping over:
And this does work, meaning the player does push the object and it seems to be a bit resistant, however Im having an odd bug.
I think due to the position/rotation being frozen, after pushing once my player will go THROUGH the object:
I cant have this. I have tried changing physics settings and set bounce threshold to 0
But still having the problem. Decreasing time step basically froze my game. Why is this happening? How can I fix it?

How to Prevent RigidBody from passing through other colliders

I've got a coin RigidBody and walls around it with box colliders. For trial purpose I've applied the following code to the coin.
private void OnMouseDown()
{
rigidbody.AddForce(30.0f, 0f, 5.0f, ForceMode.Impulse);
}
But, sometimes the coin passes through the walls, but when I increase the speed from 30 to 50 points it passes through the walls on the first click. I've googled a lot and still got nothing except for the DontGoThroughThings Script which doesn't work for me or I don't really know how to use it.
I've always added a continuous dynamic on the coin and continuous collision detection on the walls but still doesn't work.
the problem with physics collision detection is that sometimes when the speed of an object is too high (in this case as a result of a force added to the rigid body) the collision wont be detected. The reason is that the code you are executing is running at x ammount of steps per seconds so sometimes the rigidbody will go through the collider between one step to another. Lets say you have a ball at 100 miles per hour and a wall that is 1 feet wide, the code will move the ball a determined ammount of feets everytime the code is runned according the physics of it, so the movement of the ball is virtualized but its not a real physical movement so it can happen that from one step to another the ball can move from point a to b and the wall is between those points and as a result the collision will not be detected.
Possible Solutions.
You have a simple solution that wont be as accurate as it should be and a harder one that will be perfectly accurate.
The solution number one will be increasing the colliders size, according to the max speed your coin can get, that way the collider will be big enough so that the collision wont be missed between one frame to another.
The second and harder solution will be adding an auxiliar to your collision detection, some kind of a security check. For example using physical raycast. You can set a raycast to forward direction and determine if that object is an inminent collision, if it does and once the object isnt being collided by the raycast anymore then your collision detection had failed that way you have your auxiliar to confirm that and call the collision state.
I hope it helped and sorry about my english. If you didnt understound it very much i could help you with code but i will need some code from your project.
See if the colliders have their 'Is Trigger' unchecked.
Also, check if the gameObjects have colliders.
I have faced this problem many times..
Make sure Your coin's Rigidbody has "Is Kinamatic" False, Is Triggger:false,
And also "Is Trigger" of walls is False.
You could save the velocity and position on each update and then test for out of bounds, if the coin leaves the valid area you can restore it at the last valid position or plot the collision yourself if you want to

Categories

Resources