How to fix Game freeze when OnTriggerCollider? - unity3d

I have one kinematic object (player) which i move very fast (it's need), also i have another objects(obstacles) - they have collider (trigger), and when collider of player enter obstacle collider and trigger event, game freeze to half or one second.
if i reduce speed, then everything is fine
i've tried make player not kinematic and mark obstacles as not trigger (for use OnCollisionEnter event)
how i move player:
void FixedUpdate() {
if (currentWalk != Direction.NONE)
{
checkDirection(); // when distance <0.1f stop moving
transform.position = Vector3.MoveTowards(transform.position, nextPosition, speed * Time.fixedDeltaTime); //speed = 25f;
}
}
nextPosition = Vector3(0f,0f,7f); // just for example, initially player stand in 0,0,0
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "Finish")
{
success.SetActive(true); //success - any object which initially
inactive
}
}
I expect the game won't be freeze, without reduce speed, because i really don't know, what is problem, there are many game with high speed.

Related

Force occasionally being applied twice with OnCollisionEnter

I am creating a first person rocket jumping game, with the premise of shooting a rocket launcher at the players feet to move around.
I am having problems with my OnCollisionEnter function which stores all colliders in a radius and applies explosion force to them. When the player is completely on top of the explosion, force is being applied twice. Here is the code:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
}
else
{
GetComponent<AudioSource>().Stop();
Instantiate(explosionPrefab, transform.position, transform.rotation);
Vector3 explosionPos = transform.position;
//Use overlapshere to check for nearby colliders
Collider[] collidersToDestroy = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in collidersToDestroy)
{
//searches for what needs to be destroyed before applying force, this is for destructable objects
Destructible dest = hit.GetComponent<Destructible>();
if (dest != null)
{
dest.DestroyWall();
}
ExplosiveBarrel barrel = hit.GetComponent<ExplosiveBarrel>();
if (barrel != null)
{
barrel.BarrelExplode();
}
}
Collider[] collidersToMove = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in collidersToMove)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
//Add force to nearby rigidbodies
if (rb != null)
{
rb.AddExplosionForce(power * 5, explosionPos, radius, 3.0F);
if (hit.gameObject.tag == "Player")
{
//if player is hit
UnityEngine.Debug.Log("Hit");
}
}
Destroy(gameObject);
}
}
}
I can tell the force is being applied twice to the player by using UnityEngine.Debug.Log("Hit"); , which appears twice in the console. Furthermore, I am pretty sure this is happening on the same frame, as putting Destroy(gameObject); within the if (player hit) statement yields the same results.
This only occurs when the player is right next to the explosion, if the player is a small distance away the force is only applied once. I would very much like to solve this problem and have the force only applied once.
All help is greatly appreciated, thank you in advance.
I solved it!
OnCollisionEnter was being called twice because I had two colliders perfectly stacked on one another, so the projectile was hitting the 2 colliders on the same time step.

OnTrigger Events Work With Delay

I've got a bullet script with a particle system and a decal.
I think that it has something to do with these events not being able to fire in time or with fps in update. Not sure yet.
So, it's being late.
The ellow points are where the particles start to play. They should be right on these wooden walls. There should be three particles working and three bullet holes, kinda bullet penetrating one wall and getting destroyed on the second one.
THE QUESTION IS HOW TO MAKE IT WORK NORMAL, SO THAT THE TRIGGERS WORK WHEN NEEDED AS WELL AS THE PARTICLES AND THE DECALS? Maybe there's a way to excellerate the code to work on time? Or maybe there's another problem with that?
The screenshot:
The Code:
public class BulletScript : MonoBehaviour {
public bool isThrough = true;
public float BulletSpeed = 100;
public float CurrentDamage;
public float EnterLuft = -0.005f;
public float ExitLuft = 0.05f;
public GameObject woodParticle;
private ContactPoint CollisionPoint;
public GameObject BulletMarkPref;
Rigidbody bullet;
private void Start()
{
bullet = this.GetComponent<Rigidbody>();
}
void FixedUpdate () {
bullet.velocity = Vector3.forward * BulletSpeed;
//this.transform.Translate(Vector3.forward * BulletSpeed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
Transform hitPoint = this.transform;
LevelObject.LvlObjectType objectType = other.gameObject.GetComponent<LevelObject>().objType;
if(objectType == LevelObject.LvlObjectType.obstacle)
{
if (isThrough)
{
Instantiate(woodParticle, hitPoint.localPosition, Quaternion.LookRotation(-hitPoint.forward)).GetComponent<ParticleSystem>().Play();
LeaveBulletMark(this.transform, true);
}
else
{
Instantiate(woodParticle, hitPoint.localPosition, Quaternion.LookRotation(-hitPoint.forward)).GetComponent<ParticleSystem>().Play();
LeaveBulletMark(hitPoint, true);
Destroy(this.gameObject);
}
}
else if(objectType == LevelObject.LvlObjectType.obstacle)
{
Destroy(this.gameObject);
}
else if(objectType == LevelObject.LvlObjectType.wall)
{
LeaveBulletMark(hitPoint, true);
Destroy(this.gameObject);
}
}
private void OnTriggerExit(Collider other)
{
Transform hitPoint = this.transform;
Instantiate(woodParticle, hitPoint.localPosition, hitPoint.rotation).GetComponent<ParticleSystem>().Play();
LeaveBulletMark(hitPoint, false);
}
void LeaveBulletMark(Transform hitPoint, bool ifEnter)
{
GameObject TemporaryBulletMarkHandler;
TemporaryBulletMarkHandler = Instantiate(BulletMarkPref, hitPoint.localPosition, Quaternion.LookRotation(ifEnter ? hitPoint.forward : CollisionPoint.normal)) as GameObject;
isThrough = false;
TemporaryBulletMarkHandler.transform.Translate(hitPoint.forward * (ifEnter ? 0.005f : -0.005f));
}
}
I don't think your problem is something simple with the code. There is an inherent issue with calculating fast moving objects like bullets with true physics calculations especially if they are small. Often between physics updates, they pass through wall colliders completely without registering.
You have to think of it like this to see the problem. The bullet isn't tracked continuously along its trajectory. It has a starting location, a formula for its movement and it calculates a new location at each physics update. You could fire a bullet straight at a wall, and in one update the bullet would be several meters in front of the wall, and in the next, it would be several meters behind the wall without ever triggering a collision. This is why so many game use ray tracing to calculate bullet trajectories. The bullet movement isn't perfectly accurate, especially for long shots, but obstructions to the bullet path are registered.
By default unity's Physics Engine runs at 50 frames per second. A modern bullet travels between 1200 and 1700 m/s. That gives you a distance between 24 and 34 meters traveled between frames. Even a small object falling at terminal velocity (54 m/s) might pass through a collider unnoticed. If you made a 1-meter thick box collider, you would likely register a falling object but not a bullet.
I think you could do some clever combination of ray tracing and bullet physics to get the best of both worlds. Maybe you could ray trace from the bullet at each fixed update or there may be some better technique already invented for this exact situation that I don't know about.

Recommended Approach to multiple colliders

I am have some difficulty with what is probably a very silly thing. I have an enemy gameobject that depending on where it is hit (collision) - either it, or the play dies. I think the simplest way to describe this is by using the classic Super Mario Bros. game as an example.
As you all know, if the player runs into the enemy - the player will lose - UNLESS he jumps on top of the enemy's head, in which case the enemy should die.
My initial idea was to create two colliders on the gameobject:
Blue border represents a BoxCollider2D - that if collided with - will cause player to lose (notice it is slightly lower from the top)
Green border represents a BoxCollider2D on a child gameobject - that if collided with - will cause the gameobject to die.
The following is a simplified version of the code I used:
// Collider #1
public void OnCollisionEnter2D(Collision2D collision)
{
// Trigger 'Game-Over' logic
}
// Collider #2
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
Destroy(this.gameObject);
}
}
This kind-of works, however momentarily after colliding with Collider #1, Collider #2 is also trigger - and while the enemy is destroyed, the player also loses.
I have been playing with the RigidBody2D values to prevent the player from entering the 2nd collider when hitting the enemy from the top - but apparently with that force / speed, the colliders may be slightly inaccurate (or maybe I'm just doing it wrong?).
I have looked into RayCasts but this seems too complex for something that me appears rather trivial (casting rays on all four sides and four vertices of the player - assuming that the player has a box collider).
What I have resorted to 'for the moment' is a a single collider with a simple piece of code that I am unhappy with, and doesn't always work:
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
float yVelocity = collision.gameObject.transform.rigidbody2D.velocity.y;
if (yVelocity < 0)
{
Debug.Log("Enemy will lose..." + yVelocity);
Destroy(this.gameObject);
}
else
{
// Trigger 'Game-Over' logic
}
}
}
I'm currently working in 2D mode, but solutions for 3D mode (while maybe more complicated than necessary for my question) will also be considered.
Thanks guys.
as a game developer you always have many ways to solve a problem or make a gameplay.
first of all i have to say you should make a polygon collider 2d fo you objects and chracters. just colliding pictures is not very good as i see you used box cilliders in your game.
a good choice can be that you attach and empty object ot you player and set its position under foots of you player and check of enemy hit that enemy dies else if enemy hit main character object, player dies.
another choice can be when o objects collide check y position of 2 objects. of player was higher he kiils, else enemy kills the player.
if you think more you will find more answers.
you have to examin diffrent ways and find most efficient.
I think it will be easy to disable the other collider when one is triggered. You can easily enable/disable colliders with collider.enabled = true; or collider.enabled = false;
// Collider #1
public void OnCollisionEnter2D(Collision2D collision)
{
// Trigger 'Game-Over' logic
// collider2.enabled = false;
}
// Collider #2
public void OnCollisionEnter2D(Collision2D collision)
{
// collider1.enabled = false;
if (collision.gameObject.tag == "Player")
{
Destroy(this.gameObject);
}
}
This way it will be pretty lightweight and easy to implement.
One way of implementing what you want is to put each collider in its child own game object and use the IsTouching() method.
void OnTriggerEnter2D(Collider2D other){
if(GameObject.Find("Top Trigger").GetComponent<BoxCollider2D>().IsTouching(other)){
Destroy(transform.gameObject)
}
if(GameObject.Find("Bottom Trigger").GetComponent<BoxCollider2D>().IsTouching(other)){
Destroy(other.gameObject)
}
}

Avoid Pushing between rigidbody

I have a issue with my 2D game in unity (pokemon style), I'm using transform.position to move the gameobjects.
I have a player and enemies that follow him, all is ok. But when the enemies make a collision, they begin to push each other
I need that nobody to be pushed when the enemies and player get a collision.
I tried to use kinematic in enemies, but the player can push them.
I tried to add a big amount of mass to the player, but he can push the enemies.
I tried to detect the collision in code with OnCollision, but when I cancel the enemy movement, they don't return to move.
----UPDATE----
I need the collision but without pushing between them, here is a video to illustrate the problem
https://www.youtube.com/watch?v=VkgnV1NOxlw
Just for the record, i'm using A* pathfinding script (http://arongranberg.com/astar/) here my enemies move script.
void FixedUpdate () {
if(path == null)
return;
if(currentWayPoint >= path.vectorPath.Count)
return;
Vector3 wayPoint = path.vectorPath [currentWayPoint];
wayPoint.z = transform.position.z;
transform.position = Vector3.MoveTowards (transform.position, wayPoint, Time.deltaTime * speed);
float distance = Vector3.Distance (transform.position, wayPoint);
if(distance == 0){
currentWayPoint++;
}
}
----UPDATE----
Finally I'll get the expected result,changing the rigidbody2D.isKinematic property to true when the target was close and stop it
Here is a video https://www.youtube.com/watch?v=0Zm0idUU75s
And the enemy movement code
void FixedUpdate () {
if(path == null)
return;
if(currentWayPoint >= path.vectorPath.Count)
return;
float distanceTarget = Vector3.Distance (transform.position, target.position);
if (distanceTarget <= 1.5f) {
rigidbody2D.isKinematic = true;
return;
}else{
rigidbody2D.isKinematic = false;
}
Vector3 wayPoint = path.vectorPath [currentWayPoint];
wayPoint.z = transform.position.z;
transform.position = Vector3.MoveTowards (transform.position, wayPoint, Time.deltaTime * speed);
float distance = Vector3.Distance (transform.position, wayPoint);
if(distance == 0){
currentWayPoint++;
}
}
You can do this in several ways,
You can use Physics2D.IgnoreCollision
Physics2D.IgnoreCollision(someGameObject.collider2D, collider2D);
Make sure that you do the IgnoreCollision call before the collision occurs, maybe when objects instantiate.
or alternatively you can use, Layer Collision Matrix
Unity Manual provides information on using this. This simply does the collision avoidance by assigning different GameObjects to different layers. Try:
Edit->Project Settings->Physics
Or if you want it to just stop moving, You can easily do it like,
bool isCollided = false;
// when when OnCollisionEnter() is called stop moving.
//maybe write your move script like
void Move() {
if(!isCollided) {
// move logic
}
}

In Unity how to "throw" a sprite with a player if both has rigidbody2d and colliders?

I've got a player and I would like to throw an item what is "attached" to the player.
The problem is that both has rigidbody2D and Collider components. The item what I would like to throw is with the player and it has to collide with the ground and stuff. (except the player)
Here is what I tried:
if (Input.GetButtonDown ("Fire1") && canThrowCandle) {
Candle.rigidbody2D.isKinematic = false;
if (faceingRight)
Candle.rigidbody2D.AddForce(new Vector2(400f, 400f));
else if (!faceingRight)
Candle.rigidbody2D.AddForce(new Vector2(-400f, 400f));
Candle.collider2D.enabled = true;
canThrowCandle = false;
}
And then if the player collides with the item (Candle), it is with the player again:
void OnCollisionEnter2D(Collision2D coll){
if (coll.gameObject.name == "Candle") {
canThrowCandle = true;
Candle.rigidbody2D.isKinematic = true;
Candle.collider2D.enabled = false;
}
And the the code (CandleController) what controls the position of the ithem that I would like to throw:
void Update () {
if (GameObject.Find ("Player").GetComponent<PlayerController> ().canThrowCandle)
transform.position = new Vector3 (player.transform.position.x, player.transform.position.y, -0.01f);
}
So the question is, that how can I make this work?
If I understand correctly that you want the candle to go inside the player but both of them need to collide with the ground.
This can be done using collision layers. Just put ground, player and candle to different layers. Then adjust that both player and candle are colliding with ground, but not with each other.
If you want both trigger collider and physics collider to be attached to same gameObject, I think you need to use child gameObject for that as mentioned here.