Home > Net >  Collisions between objects?
Collisions between objects?

Time:10-08

My problem looks like this: I have one stationary object wall and second object that is moving projectile. I want projectile to stop when it collides with wall. But ewery my attempt ended with projectile passing through wall.

Only script that affects movement of projectile:

public Transform trans;
void Update()
{
     trans.Translate(0, 0, speed * Time.deltaTime, Space.Self);
}

All compomponents of projectile:

enter image description here

All components of wall:

enter image description here

Note: I have correctly set up layers in project settings.

I have tried many combinations of rigit boddies, colliders, layers and so on, but projectile always passed through wall.

CodePudding user response:

In general, your presented setup is right. The contacting object have a Rigidbody component, and the objects you can collide have at least a collider (not a trigger).

First possible problem: your Rigidbody is set to Kinematic. Did you enable all collision modes in Project Settings > Physics > Contact Pairs Mode?

Second problem is the use of transform.Translate in the Update. Translate will ignore physic and just move the object when you tell it to do, and Update may be out of sync with the physic loop, where it would be recommended to use FixedUpdate. Even using FixedUpdate, Translate will not do the right job. It will force the object to move and the physic can complain or not about that (it can move the object back, glitching, or it can just ignore and let the object pass).

The right way to move a Kinematic rigidbody is through rigidbody.MovePosition, as stated in the Unity docs. For non-kinematic objects, you can use AddForce. Some tutorials also teach to set velocity of Rigidbody, which is not recommended because it can have undesired effects. Do it if you know the side effects and really want them.

CodePudding user response:

I had a similar issue before. May I suggest destroying the projectile instead? You will then have to use OnCollisionEnter().

First, create a script and attach it to your projectile game object. After that, give your wall a tag, like 'wall'. Then, do something like this:

void OnCollisionEnter(Collision collision)
{
    // Gets information about the game object the projectile collided with
    GameObject objectCollided = collision.gameObject;
    if (objectCollided.tag == "wall'){
         // Do something with either the wall/enemy
    }
    // Destroy the projectile
    Destroy(gameObject);
}

Note: If the intent was not to destroy the projectile but rather sticking to the wall, I suggest you to use detect collision using Raycast. Raycast has a hitInfo parameter and stores information about a gameObject that it hit, as well as the where did the Ray hit the gameObject.

Thus you can use this to move your projectile accordingly. In my previous projects, I used the Rigidbody component of my projectile and used .AddForce() to shoot it out, then detect collision with OnCollisionEnter().

Maybe you could do the same, but have a separate function to remove that force on entering a collision. I never tried it so I do not know if it will work.

  • Related