2D Toolkit Forum
2D Toolkit => Support => Topic started by: arvz on February 19, 2014, 12:58:19 pm
-
Hey,
I'm trying to implement a throwing knife with rope attached to it. It looks like this:
(http://i.imgur.com/TFQ9OO8.png)
The left rope sprite is a tk2d tiled sprite, and the knife on the right is an object the character is able to throw in different directions. The anchor of the rope stays wherever the character is, but the middle handle of the other side of the rope needs to keep itself exactly where the knife's handle is at. This translates to modifying the tk2d tiled sprite dimensions.x value which is in pixel units, whereas the movement of the knife will be in Unity units (whatever it is called!)
What might I be able to do to keep these 2 points together? I feel like there is some kind of formula I can use.
Not sure if it's important, but I am using 100 pixels per meter on my tk2dcamera.
Or is there a complete other way I can implement this that is much easier?
Thanks
-
I think the easiest way is to make 2 sprites: one for the knife and one for the rope.
Then let the rope sprite increase in the x-pos the further the knife gets from the character. Mean while start tiling the rope sprite along the x-pos.
-
It is already two sprites of course, the knife is its own sprite while the rope is a tiled sprite
What you've said is exactly the same as what I said except just reversed. Even if I keep the anchor of the rope on the knife I still need to extend the dimension.x value but in relation to the character instead of the knife.
-
It is already two sprites of course, the knife is its own sprite while the rope is a tiled sprite
What you've said is exactly the same as what I said except just reversed. Even if I keep the anchor of the rope on the knife I still need to extend the dimension.x value but in relation to the character instead of the knife.
Uhm, now I look at it again, you're right (was pretty tired earlier today :-\ ).
Okay, algorithm then (not programming, just working it out in words leaving the programming to you :D )
Calculate distance between char.x and knife.x. In the middle drop the rope sprite and set rope.x length to knife.x-char.x (there's a special distance function for it in Vector iirc - makes it easier when you can throw the knife over the y-axis as well).
-
dimensions is in texel units. Assuming the anchor of the tiled sprite is the middle left (important, so adding width will simply extend it towards the right), you need to calculate the dimensions.y value that is required, which should be world_distance_between_sprites / tiledSprite.CurrentSprite.texelSize.y.
-
The anchor of the tiled sprite is indeed middle left.
you need to calculate the dimensions.y value that is required
Do you mean dimensions.x here? I don't see the y needs to be calculated as the y value is just the height of the sprite which in this case is 54 pixels and will stay constant. Only the length (dimensions.x) changes as it gets longer
world_distance_between_sprites / tiledSprite.CurrentSprite.texelSize.y.
'world_distance_between_sprites' Is this simply the distance between the knife and the anchor point of the tiled sprite?
And again, why is it texelSize.y?
-
I was assuming it was facing up. Feel free to swap with x, there should be no difference.
The world distance between sprites = (a.transform.position - b.transform.position).magnitude
-
Ok, I tried the formula you suggested:
float newDimensionX = (transform.position - knife.transform.position).magnitude / ropeSprite.CurrentSprite.texelSize.x;
Vector2 newDimensions = new Vector2(newDimensionX, ropeSprite.dimensions.y);
ropeSprite.dimensions = newDimensions;
however it ends up looking like this: (The rope is not long enough. The pink line is a linerenderer I created for debugging purposes)
(http://i.imgur.com/w775hks.png)
What do I put as the initial size for dimension.x? The sprite itself is 58x54 so I set to x=58 y=54. Does this even matter? (Trying different values doesn't seem to make a difference)
-
Are you using local scale or scaling in any other way?
-
Actually, yes, I have set the x scale to 0.2. Have set this back to 1 now and seems to be working, thanks! :)