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

Allowing player to draw circle around enemies. Best method?

Discussion in 'Scripting' started by Hoskins, Nov 15, 2014.

  1. Hoskins

    Hoskins

    Joined:
    Nov 15, 2014
    Posts:
    2
    Hi all,

    I hope I'm posting this in the right forum, feel free to move it if I haven't.

    I'm currently trying to design a mechanic where the player can run around an enemy. As the player runs around, their positions are saved. Should the game detect that the player has ran an enclosed shape (say, a rough circle), any enemy inside that area will be destroyed.

    For an example of what I want to do, this video here shows an old game which does a similar thing:


    I've been trying to figure out the best way to do this. People have suggested creating a mesh, but I think that may be too complex for this, especially since I won't know what the shape will be.

    Does anyone have suggestions on how to pull this off?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Make note of points as the player moves, create line segments between them. If a new segment intersects an existing one, you have formed a closed shape. Intersection point is a new point needed to create shape. Then find if other points are inside it. Remove oldest segments once you reach a certain number.
    There are algos for all this easily searchable with google (line intersection, point within polygon)
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    You can use PolyContainsPoint for detecting what's inside the closed shape, once you have that (just loop through the positions of any existing enemies), and Vectrosity for the line drawing.

    --Eric
     
  4. Hoskins

    Hoskins

    Joined:
    Nov 15, 2014
    Posts:
    2
    Thanks for the suggestions, I'll look into them.

    What of actually creating the shape, though? Is creating a mesh from the points the best way to go about it? I'm worried it may be a bit too complex considering I won't know what the shape will be.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    That's what Vectrosity does.

    --Eric
     
  6. hunterjett2228

    hunterjett2228

    Joined:
    Oct 20, 2018
    Posts:
    5
    Sorry to reply to such an old reply but I'm trying to do a similar thing to what the OP was doing and I am very new to coding and scripting could you explain a little more as to what the script would look like if you did exactly what the gif is doing? or send me to a link where i could find such explanations. thank you!