- Home /
Stretch sprite to a position (finger position)
I have a Sprite which is a wire. I want the player to be able to touch and drag the sprite such that the wire will stretch to where the player's finger is. The left side of the wire will be affixed to a wall. I have the sprite's anchor point set to the left, so I believe that's a start, but I don't know how else to proceed.
The effect I am looking for is similar to this: http://youtu.be/pVKFGTk5t4M?t=42s
Answer by robertbu · Aug 27, 2014 at 03:40 AM
I'll give you a starter recipe that you can build on. Beyond the stretching and angling, in that video the texture of the rope is being tiled. For that reason, I'm going to suggest you not use a Sprite, but instead use a special plane. So:
Start a new scene
Set the camera to Orthographic
Get the CreatePlane editor script from the UnityWiki. It must be named CreatePlane.cs and it must be put in the Assets/Editor folder.
Do 'GameObject > Create Other > Custom Plane...'
Create a plane that is 'vertical' and has the Anchor that is 'LeftHalf'.
Create a new material
Set the shader to Unlit/Transparent
Save the image below to the project
Change the 'Format' in the Inspector Import Settings for the rope texture to 'Truecolor'
Select this texture for the material
Apply the material to the custom plane you created
Attach the script below to the plane
Run, click the mouse and move the mouse around while the button is held down
#pragma strict
function Start () {
transform.localScale.x = 0.0f;
}
function Update () {
if (Input.GetMouseButton(0)) {
var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = 0.0;
var dir = pos - transform.position;
transform.rotation = Quaternion.FromToRotation(Vector3.right, dir);
transform.localScale.x = dir.magnitude;
renderer.material.mainTextureScale = Vector2(dir.magnitude, 1.0);
}
}
For a limited time, you will find a Unity Package:
Your answer
Follow this Question
Related Questions
Sprite Touch and Drag in 2dToolkit 1 Answer
Object dragging at manual point 2 Answers
unity2D touch drag 0 Answers
Unity2D iOS Dragable items by touch 0 Answers
Help to fix my drag code [SOLVED] 1 Answer