Search Unity

Transform.Translate not functioning

Discussion in 'Scripting' started by ErnestoBananez, Dec 14, 2015.

  1. ErnestoBananez

    ErnestoBananez

    Joined:
    Sep 23, 2014
    Posts:
    16
    Hi everyone. This post is somewhat tied to my previous forum post here (and is actually mentioned within it). Basically I'm trying to build my own gravity and collisions in Unity, and to do that I'm casting rays downward from the (vertical) center of an object, and basing their length on the object's downward velocity. When a ray hits something, I want 1) the other rays to stop firing, 2) the object to move flush with the object it's just collided with and 3) the velocity.y to become zero.
    Rays.png
    So far, only 1 and 3 are happening at all. I'm using

    //note: subtracting box.height/2 is done since the raycast comes from the collider's center.
    // the box is essentially a Rect with the info of the object's collider set to it every frame.
    transform.Translate(Vector2.down * (hitInfo.distance - box.height / 2));

    in order to translate the object to be flush with the surfact it's contacting, but that just doesn't happen. Instead, the falling object stops exactly where it is when it comes into contact with the 'ground', the velocity is cut as intended, and then it starts falling again until the raycast (which has been shortened since it's based on the downward velocity) connects with the object below it again, at which point its velocity is set to zero again, at which point it starts falling again, etcetera, etcetera... eventually it does end up flush with the object below it, but it happens in a messy, glitchy way and I don't understand why.

    If anyone could shed some light on this or try to explain what I'm misunderstanding I'd be very grateful. If you'd like to see the code causing this to happen or what the object's inspector looks like, you can find both in the post I linked above. Thanks!
     
    Last edited: Dec 14, 2015
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    One thing I notice is that you are doing your work in FixedUpdate(), yet you are using Time.deltaTime. I think you want to be using Time.fixedDeltaTime when you are in that function.

    That said, I'm not sure this would cause the problem you are seeing.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    In FixedUpdate, Time.deltaTime is Time.fixedDeltaTime. Docs I don't know the cause either, but Transform.Translate definitely does function, so that's not the problem.

    --Eric
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    so once it's collided the raycast is set to length 0 (vel 0) and the raycast is no longer detecting the "floor"... so it starts falling again?
     
  5. ErnestoBananez

    ErnestoBananez

    Joined:
    Sep 23, 2014
    Posts:
    16
    Yeah I'm using transform.translate to move it flush with the object it's colliding with (that's what's not happening) and then setting the velocity.y to zero so it doesn't try moving through the floor.