Search Unity

Character's path visual representation

Discussion in 'Scripting' started by mckertis, Dec 23, 2009.

  1. mckertis

    mckertis

    Joined:
    Dec 20, 2009
    Posts:
    16
    I have a character, when i select him and click on a random spot on the terrain i want to create an arrow, that will point from under the character to the point where he will move. Naturally, the arrow's length is arbitrary, so i cant just use a sprite...how would a Real Programmer (tm) go about doing stuff like that ?

    Also, two optional things with the arrow that i'm also interested in :
    1) How to make it "stick" to the terrain, following its ups and downs, and
    2) Click and drag a random point on the arrow to make it into a curved arrow.
     
  2. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    The way I would do it is to load a 3D model of a simple plain that has a texture of an arrow on it.

    1. Instantiate it at the player's position, assuming the player's pivot point is at it's feet
    2. Attach a lookAt script to rotate it to the position you clicked on
    3. Scale it to match the distance...

    In terms of making it stick to the terrain, i would take the model of the plane and make it a series of quads. At each intersection I would attach an empty node. In Unity I would attach a script to each node. This script simply calculates the distance between it and the ground plane and then adjusts it's height to match. So the more sections your model has, the smoother it will follow the terrain.

    If you would like to make the arrow shrink as the player moves towards the goal, I would suggest making the arrow point the other way, starting from the position you clicked and looking towards the player. Actually, i would recommend it this way, anyway, but that is just personal preference...

    Regarding the arbitrary length, you could always use 2 planes. One contains the arrow head and a node at the end. The second plane just contains the body of the arrow and an node at the end.

    When you instantiate your arrow, create a new Array() object, place the head into Arrow[0] and then simply create a loop to populate the rest of the array with a series of instances of the second plane. Attach each plane to the node of the arrow at Arrow[Allow.length-1] and you are done.

    Of course, if you create a prefab of this plane and attach that heightmap script I mentioned before, it will automatically fit the terrain... If you then also attach a collider to the prefab and test against the player touching it, you can destroy each node as the player reaches the next, thereby making your arrow automatically shrink as your character moves...
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It would be much simpler and more exact to use a projector, but also potentially slower, as the terrain has to be rendered twice in that case.

    --Eric
     
  4. mckertis

    mckertis

    Joined:
    Dec 20, 2009
    Posts:
    16
    First of all, if i do that - the arrow will be created with the character at the middle of it, which is undesirable. Second, if i scale the sprite with the arrow - it will scale the body of the arrow, but it would also scale the arrow head, which would be very ugly...so at the least i cant do it this way with just one plane, it would require at least separate body and head of the arrow, but that creates more problems, like with alpha tunneling, and terrain-sticking...
     
  5. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    To make it stick to the terrain I can only think of two possible ways to do it, but both require the had and body to be separate. The first method I already explained above. The alternative is to create the same model but instead of using planes, use very thin cubes so yo won't have to worry about using dual sided textures and the planes not being drawn... Then simply attach a collider to each and just drop it where you instantiate it and let the physics take care of the rest. I still think the first way is the better option. Personal choice.

    With regards to the character in the center of the arrow, that is up to the 3D model's pivot points. If your character's vipot point is at it's feet and the pivot point for the arrow is places 1000 units below the edge of the arrow, when you instantiate it it will actually start 1km above your character's head. :p Just adjust the pivot in the model and you can make it start at your character's feet.

    You asked how a programmer would do it. This is my way. If it is not what you were expecting, sorry i couldn't help out :)

    /me takes 3 steps back
    /me turns to the side and yells: "Next batter's up!"

    :D
     
  6. mckertis

    mckertis

    Joined:
    Dec 20, 2009
    Posts:
    16
    I'll try fiddling with these ideas now, will write here later with results, if any.
     
  7. mckertis

    mckertis

    Joined:
    Dec 20, 2009
    Posts:
    16
    Okay, i ran into a little problem. So i made this quad, moved its pivot precisely to the edge, positioned it in Unity at 0,0,0. Referenced it to a character, which is also standing at 0,0,0. Then, when i point to the ground and scale the quad - if i point in front of the character the quad scales a little further than the point, and if i point behind the character the quad scales slightly less than needed. Whats going on ?

    What i'm doing is, first finding the length between where i'm pointing and the center of a character via scaleRatio=Sqrt(Sqr(b.x-a.x)+ well, you know how it goes, then doing transform.localScale.z = scaleRatio/quad z length.