Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Rigidbody moves faster when below a certain line

Discussion in 'Physics' started by leapformula4, Feb 23, 2020.

  1. leapformula4

    leapformula4

    Joined:
    Mar 26, 2014
    Posts:
    50
    I am building a 2D top-down space game, with a similar art style as Transcendence.

    Each ship has a rigidbody, and all the ships travel along a 2D plane, even though they appear to be in 3D space.

    The issue is that when a ship travels below a certain line in the scene, they speed up as if the ship has no mass or drag. When the ship travels back above that line, they slow back down.

    This behavior is noticeable because the ships travel faster than their bullets when they speed up. Here is an image of the scene, where the white line at the bottom is a portion of the Canvas.

    overview.PNG

    Here is the ship's Inspector view:

    cab-inspector.PNG

    This behavior occurs for both AI and player ships.
    My movement code is:

    Code (CSharp):
    1. Vector3 moveDirection = Vector3.Normalize(transform.position - targetPosition);
    2. rigidBody.AddForce(-7 * moveSpeed * moveDirection);
    I can see by debugging, that rigidBody.velocity does not change even after the ship speeds up.

    Moving the original position of the ships does not change the result, once they travel below a certain line (relative to starting position), all ships move much faster than they should.

    Please help me find the cause of this issue.
     
  2. ATLGAN

    ATLGAN

    Joined:
    Oct 15, 2017
    Posts:
    10
    Did you try use ForceMode? If you don't, you can use ForceMode.VelocityChange.
    Code (CSharp):
    1. Vector3 moveDirection = Vector3.Normalize(transform.position - targetPosition);
    2. rigidBody.AddForce(-7 * moveSpeed * moveDirection,ForceMode.VelocityChange);
     
  3. leapformula4

    leapformula4

    Joined:
    Mar 26, 2014
    Posts:
    50
    Hello ATLGAN, I tried using ForceMode.VelocityChange. However, whenever a ship goes below the line (X,Y,Z) = (_,_,0) the ships accelerate dramatically.

    I'm starting to think that the best way to fix this issue is to drag the starting position to (_,_,10000) and apply a border to the world
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Where are you calling your physics code. In FixedUpdate, right?
     
  5. leapformula4

    leapformula4

    Joined:
    Mar 26, 2014
    Posts:
    50
    yep, methods in FixedUpdate() call ManeuverToPosition() which contains movement code

    also, if I check "is trigger" for the capsule collider, the ships moves normally at the Z=0 line. I use the capsule collider to prevent ships from overlapping

    The ships' capsule colliders "lay down" instead of "stand up". Could this be the cause of the issue? I also have separate bullet colliders (the squares inside the capule)

    ship.PNG
     
    Last edited: Feb 25, 2020
  6. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    There is really no way I can imagine that would make rigidbodies behave differently just because they cross a line unless you specifically coded this behavior.

    what helps me in situations like this is to draw the forces on some bodies with Debug.DrawLine(). Call it where you add the force from the position of your spaceship to position+force. Maybe you have to scale the lines down. See if the force actually jumps when the body goes below the z=0 line.

    Also check if there is any way that AddForce could get called multiple times (maybe in different locations?) during a physics frame.
     
  7. ATLGAN

    ATLGAN

    Joined:
    Oct 15, 2017
    Posts:
    10
    The AddForce method has a general tendency to accelerate. I solved this using ForceMode.VelocityChange.If you don't solve this issue, you can also try Rigidbody.MovePosition(x,y,z).
     
  8. leapformula4

    leapformula4

    Joined:
    Mar 26, 2014
    Posts:
    50
    @ATLGAN - I need to have collision to prevent ships from overlapping. I think that won't work with MovePosition, which will teleport ships without checking for collisions

    @tjmaul - Even if addForce gets called multiple times, it would noticeably affect the value of the velocity, except velocity doesn't increase even when the ship speeds up past the Z=0 line (checked by printing the magnitude of velocity to console every FixedUpdate).

    "Fixed" by putting colliders at the edges of the scene, and moving the planets into the center of the box.