Search Unity

Projectiles passing through

Discussion in 'Scripting' started by yangmeng, Dec 29, 2006.

  1. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    I am using this script from the Unity docs to move a "projectile" that is being instantiated:
    clone.velocity = transform.TransformDirection (Vector3.forward * 10);
    The projectile sometimes passes right through objects with colliders and sometimes hits them. Is this a Time.deltaTime issue? If so how would I add Time.deltaTime? I tried adding it like this "(Vector3.forward * 10 * Time.deltaTime)" but that seems to stop the projectile from being instantiated.
    I am missing something here, but can't figure it out. I'd be grateful for any help.
    Thanks.
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    What is clone? I am guessing that it is a rigidbody. No, that is not a deltaTime issue at all. I recomend using raycasts for projectiles as they will never go through things. Basicly you decide how far the bullet will travel this frame, shoot a ray that far forward and if it doesn't hit, then move it that far.
     
  3. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    I recently talked with someone from Havok at a conference about this issue and in that engine they interpolate the collision calculations at a variable resolution depending on the speed of the object so that they always detect collisions. I don't know if PhysX does something similar. The problem comes from the projectile moving through the object in a single time step, increasing the number of physics time steps should also fix this.

    --Jeff
     
  4. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    The issue here is that you are updating the projectile position manually, which overrides any physics including collision detection.

    You are moving the projectile 10 units on every update --- it does not interpolate automatically as TransformDirection simply sets the position directly. Either allow the physics to control the position itself and either set rigibody.velocity or ditch physics entirely and use raycasting instead as Yoggy suggested.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Actually, in his example, he is setting the rigidbody's velocity.

    I agree with Yoggy - for anything very fast-moving, raycasts + manual movement is the way to go.
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Ah. I misread that. Then the solution is to either reduce the speed and/or increase the physics time step setting. Or to do it with raycasts and no physics.
     
  7. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Thanks for your help. I was hoping it was a simpler problem. I will look into the physics time step solution and see how well that works.