Search Unity

Question Unity 2D Spring-like joint sprite

Discussion in '2D' started by lizukadaky101, Feb 13, 2023.

  1. lizukadaky101

    lizukadaky101

    Joined:
    Sep 23, 2021
    Posts:
    4
    Hello, I have two draggable game objects linked together using spring joint component. I also have a line renderer that connects them with a line (imma post the code down below) and I need to make the line wavy, like an actual spring would be. Any ideas on how should I go about that? Keep in mind im still kinda a newbie to unity and I dont have good programing skillz lol.



    The code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Line : MonoBehaviour
    4. {
    5.  
    6.     public GameObject gameObject1;          // Reference to the first GameObject
    7.     public GameObject gameObject2;          // Reference to the second GameObject
    8.    
    9.  
    10.     private LineRenderer line;                           // Line Renderer
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.         // Add a Line Renderer to the GameObject
    16.         line = this.gameObject.AddComponent<LineRenderer>();
    17.         // Set the width of the Line Renderer
    18.         line.SetWidth(0.05F, 0.05F);
    19.         // Set the number of vertex fo the Line Renderer
    20.         line.SetVertexCount(2);
    21.             }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.  
    27.         // Check if the GameObjects are not null
    28.         if (gameObject1 != null && gameObject2 != null)
    29.         {
    30.             // Update position of the two vertex of the Line Renderer
    31.             line.SetPosition(0, gameObject1.transform.position);
    32.             line.SetPosition(1, gameObject2.transform.position);
    33.         }
    34.     }
    35.  
    36.  
    37.  
    38.    
    39.  
    40.  
    41. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    As weird as it sounds you can get certain types of springy motion out of a chain of FixedJoint2D objects.

    Try this: make 3 or 4 sprites in a row, put Rigidbody2D and Collider2Ds (of some kind) on them.

    Now link the last one to the previous one with a FixedJoint2D, set the frequency to 1 or 2 in the joint.

    Do the same for all the others until you reach the first one.

    Put a 2D surface below them (or just a pointy fixed 2D collider) and press PLAY

    Springy boingy, super-cheap and cheerful.

    Play with the frequency to get different feels. Higher is stiffer.
     
  3. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Here are 3 different approaches it could be done.
    * create new material and set a wavy texture, assign it to your line renderer have the texture mode set to strech. No changes in your code required.
    * instead of line renderer consisting of 2 points, generate a bunch of points between. You will need to know how to use loops (to iterate over all the points), slerp to get points between 2 endpoints, Vector2D.Perpendicular to get vector perpendicular to spring direction, use Mathf.Sin or Mathf.PingPong to get back and forth movement
    * instead of line renderer use a sprite renderer with sprite that contains desired amount of curves, stretch it by assigning size property. You will need a little bit of math to calculate sprite position and angle from spring endpoints.
     
  4. lizukadaky101

    lizukadaky101

    Joined:
    Sep 23, 2021
    Posts:
    4

    I dunno whether Im doing something wrong but in my case it breaks the Spring Joint 2D component that I have on the draggable objects, same goes for the 2D surface - it collides with the draggables. Is there a way around that? I tried putting them on different layers but that didnt seem to help.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Anything in the physics world you want to move needs a Rigidbody2D, either simulated or in the case of platforms and movable ground and whatnot, untick the simulated boolean.

    Then whenever you move anything, always consider this:

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121