Search Unity

2D Line Renderer with physics

Discussion in 'Physics' started by spikeythepilot_unity, Jun 9, 2019.

  1. spikeythepilot_unity

    spikeythepilot_unity

    Joined:
    Jun 9, 2019
    Posts:
    4
    Hi everyone

    I'm looking for some advice on the correct way to add physics to a 2d line renderer as I am seeing some strange physics.

    I can create a line using the line renderer and add edge detection which interacts correctly with other RigidBody2d objects. If the then make the line renderer also have a RigidBody2d so it is affected by gravity the line doesn't move unless I uncheck the line renderers 'not Use World Space' checkbox.

    Now the line renderer falls under gravity and interacts with other rigidBody2d objects but the physics is all messed up. It looks like the line renderer centre of mass appears to be related to world coordinates but the line renderer is moved in local space. The further away from x=0 the line renderer moves the worst the messed up physics becomes.

    For example: if I try and balance a horizontal line renderer on a static object it only works correctly if the static object is x=0. If the test is repeated with static and line renderer moved left (x<0) then the horizontal line renderer doesn't balance but instead flips quickly to the right. Repeating with the static object and line renderer moved right (x>0) then the horizontal line renderer doesn't balance but instead flips quickly to the left.

    Is this physics problem caused by setting the line renderer to not use world space? If it is, then what is the correct way to make a rigidbody2d move a line renderer with the line renderer using world space?

    Any help or suggestions are appreciated, thanks in advance.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,056
    Physics has no interaction with the line-renderer whatsoever. A Rigidbody2D changes the Transform so a line-renderer set to use local space should just render at that Transform pose.
     
  3. spikeythepilot_unity

    spikeythepilot_unity

    Joined:
    Jun 9, 2019
    Posts:
    4
    Thanks for the advice.
    I have since solved my problem. I looked at the centre of mass variables for the rigidbody attached to the line renderer and it was always set at 0.0 regardless of where the line renderer/rigidbody was created. I incorrectly assumed the centre of mass was calculated automatically.

    I now calculate the centre of mass manually and it is now all working with the correct physics.
     
    Pulkitguglani1011 likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,056
    It is calculated automatically but based upon the colliders. Renderers have no impact on physics as their job is to render stuff.
     
    Pulkitguglani1011 likes this.
  5. Pulkitguglani1011

    Pulkitguglani1011

    Joined:
    Nov 24, 2018
    Posts:
    5
    How did you calculate the local center of mass for that line?
    Sorry but I am a beginner in physics stuff.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,056
    A quick way would be to find the mean of all vertices.
     
    Pulkitguglani1011 likes this.
  7. Pulkitguglani1011

    Pulkitguglani1011

    Joined:
    Nov 24, 2018
    Posts:
    5
    Ohh Yes , I thought it would be so hard ... Thanks for the help.