Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

What would i use to display move ranges?

Discussion in 'Scripting' started by riadsala, Feb 10, 2010.

  1. riadsala

    riadsala

    Joined:
    Feb 10, 2010
    Posts:
    15
    Sorry for not giving this post a very good title! If i knew what I was after, I wouldn't need to ask!

    Anyway:

    I'm teaching myself the basics of Unity and am trying to a simple boardgame. At the moment, I've made a simple board (a cuboid) and a playing piece (a cylinder).

    And I've worked out how to make the cylinder respond to keypresses, mouseclicks, etc (at the moment, a partcile effect goes off while you mouseover and click on it)

    What I would like is a way if displaying how far I can move the piece when I click on it... ie, highlight everywhere within r units of the piece.

    The only thing I can think of is to create a cylinder with radius r and centre it on the playing piece when it is clicked. However, this doesn't seem very elegent and I would run into trouble when my piece was near the edge of the board, or if there was another piece in the way, etc.

    I've had a look through a few tutorials, but I haven't found anything relevant.


    Thanks :D
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    Can the playing piece move anywhere within the range or is it restricted to horizontal, vertical and diagonal?
     
  3. riadsala

    riadsala

    Joined:
    Feb 10, 2010
    Posts:
    15
    Hey, thanks for the reply, and the warm welcome :)

    As for your question: I'd probably like to know how to do both types of behaviour. I'm making sure I get my head round most the scripting stuff I'll want before worrying about actually designing a game!
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Rather than give a longwinded answer, I thought you'd probably rather see some code, so I've attached a script that implements a couple of classes. One represents a grid of a certain width and height, the other a position on that grid. The position class ("GridPos") then has some methods to tell if two squares are on the same column, row or diagonal and what is the distance between them.

    The purpose of the other class ("Grid") is to convert between an XY grid position and an index number. If you have a grid of, say, 64 squares, it is handy to be able to assign each its own number in the range 0 to 63. This number is most useful as an index into an array.

    Unless the number of squares is huge, you should be able to iterate through all of them and test whether each one is within a certain distance or on the same column, row or diagonal as a target square. When you've established the set of squares you want to highlight, you can do so by placing a marker object onto each one or whatever other method is appropriate to the game.
     

    Attached Files:

  5. riadsala

    riadsala

    Joined:
    Feb 10, 2010
    Posts:
    15
    Wow. How incredibly helpful!

    I'll have a look over the code over the weekend :)

    And I'm amused to see the file's been downloaded a few times already!

    I'll let you know how I get on.
     
  6. riadsala

    riadsala

    Joined:
    Feb 10, 2010
    Posts:
    15

    Hey it's me again! While i haven't had much time to have a proper look at your code, i have a question. Basically, could you expand on the quote above... by marker object, do you mean creating geometry (like, a small circle/square) on squares which are in range?

    While I can see how to do this, it isn't quite what I was wanting (although, now I think about it, designing my game around a kind of large chess board might be a good design choice, it'll no doubt simply things a bit and help make my humble effort stand out!)

    I guess I was more thinking of the way movement range is implemented in the Total War campaign maps. I assume the map is still grid based, just at a finer level. but quite often, because of terrain, the distance you can move in a turn is an odd shape, with a "squiggly" perimeter.

    Any idea what I would use to highlight a region? ie, once I've carried out the grid/distance calculations, and taken terrain into account, what's the best way to display it?

    Thanks
    Alasdair