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. Dismiss Notice

Question How do I make a sprite "follow" another?

Discussion in 'Scripting' started by Abulero, Sep 19, 2023.

  1. Abulero

    Abulero

    Joined:
    Dec 18, 2020
    Posts:
    9
    Hey all. I a laser skill in a game I'm developing and I want to improve how it looks but don't know how to accomplish that.

    When it's activated, a trail is marked on the floor where the laser will fire. After some time, the laser grows from onde end to the other, damaging the enemies, after which the trail changes color to red and then everything goes away.
    The way I accomplish this is by having the laser have a pivot on its left side while having a circle collider on its right side, which triggers the damage. During 1 second, I make the laser's transform's scale go from 0 to 1.
    However, I would like to make it look a little better by showing a laser coming from above, actually scorching the ground from one end to another. I have no idea how to accomplish that, though.
    In the link below, you can see how the skill looks now and what I want to add.

    https://imgur.com/a/oFhDS0i

    The laser coming from above has a fixed left end while its right end follows the ground laser's right end, stretching itself.
    Does anyone have any suggestions?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    If i understand correctly, you want a beam to follow that red path but appear as if it is coming from above? Something like this amazing piece of artwork i worked really hard at: upload_2023-9-19_9-59-46.png

    If i am on the right path, i would say just create some sort of cylinder (for prototyping the feasibility of it) that then follows that line. You put a collider on the cylinder and that is what would deal damage. Maybe that helps?
     
  3. Abulero

    Abulero

    Joined:
    Dec 18, 2020
    Posts:
    9
    The issue is that I also need 2 things to happen:

    1. One of the ends of the cylinder you mentioned should be fixed in the same position
    2. The other end should follow the collider

    The end result is that the cilinder ends up stretching. I could accomplish this by calculating the correct angle and horizontal scale of the cilinder as a function of the collider's position, but I'm looking for a simpler solution :(
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Ahhh, you want more of a moving wall effect instead of a single beam of light effect. I think you could just have a large "wall" sprite that moves along that path instead. That way you wouldnt have to stretch anything. Maybe like this?
    upload_2023-9-19_11-22-33.png
     
  5. Abulero

    Abulero

    Joined:
    Dec 18, 2020
    Posts:
    9
    Actually like this ! (Sorry, I can't rival your paint skills)

    upload_2023-9-19_18-31-51.png
     
  6. Abulero

    Abulero

    Joined:
    Dec 18, 2020
    Posts:
    9
    The horizontal ray already works. What I need is the one coming from above to have the left end fixed, and the right end follow the right end of the horizontal ray
     
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Ahhh gotcha. You will probably have to do a bit of relatively simple math to determine the spawn point of the "above" ray start depending on the rotation of your player or the enemy or the screen or w/e you want to determine it from. Basically, if ray starting from left, then position the "above" ray start by AMOUNT on Y axis. Then you use a line renderer or w/e to draw a line from the left start pos to the right end pos of the first ray. So in update you would have logic to say: endPosOfTopRay.transform.position = endPosOfBottomRay.transform.position or something like that.

    This is probably overkill and someone else may have a better way to implement it.

    But for some unsolicited advice, I would move on from this if you cant get it figured out in a reasonable amount of time. It appears you have this working already, but just not the looks. Get all of the functionality you want and when everything is working from a functional standpoint, THEN go back to updating graphics, animations, particles, etc. Its just a tip for assisting with rapid prototyping which i highly recommend.
     
  8. Abulero

    Abulero

    Joined:
    Dec 18, 2020
    Posts:
    9
    Yeah, I thought there might be a way to simply "lock" a sprites point to another so that it follows it and stretches accordingly. But I think I'll indeed have to go through the math route.

    And I think your tip makes lots of sense. I'll definitely do that.

    Thanks for the help ! :)