Search Unity

Curve and stretch a sprite to mouse click/touch

Discussion in '2D' started by royki, Oct 17, 2019.

  1. royki

    royki

    Joined:
    Oct 17, 2019
    Posts:
    1
    Hey, I wish to create a game object where you can start dragging it by touching somewhere on the line of its collision and to create these "dynamic shapes" by stretching the sprites and readjusting the sprite look and collision according to the drag point. Adding an illustration for clearance:

    Started playing with `Sprite Shape Renderer` to create these curved sprite tiles but I need to be able to create dynamic ones using the mouse cursor and adjust all collisions.

    I've tried to add an `AnchorDragger` script to the `Sprite Shape Renderer` object:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.U2D;
    6.  
    7. public class AnchorDragger : MonoBehaviour
    8. {
    9.    const int INVALLID_INSERTED_POINT_INDEX = -1;
    10.  
    11.    public SpriteShapeController spriteShapeController;
    12.    private Spline spline;
    13.    private int inseretedPointIndex = INVALLID_INSERTED_POINT_INDEX;
    14.  
    15.    void Start()
    16.    {
    17.        spline = spriteShapeController.spline;
    18.        int pointCount = spline.GetPointCount();
    19.        for (var i = 0; i < pointCount; i++)
    20.        {
    21.            Vector3 currentPointPos = spline.GetPosition(i);
    22.            Debug.Log("Point " + i + " position: " + currentPointPos);
    23.        }
    24.    }
    25.  
    26.    // Update is called once per frame
    27.    void Update()
    28.    {
    29.        if (inseretedPointIndex != INVALLID_INSERTED_POINT_INDEX)
    30.        {
    31.            spline = spriteShapeController.spline;
    32.            spline.SetPosition(inseretedPointIndex, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f)));
    33.            // Debug.Log("Setting position to: " + Camera.main.ScreenToWorldPoint(Input.mousePosition));
    34.        }
    35.    }
    36.  
    37.    void OnMouseDown()
    38.    {
    39.        Debug.Log("Mouse Down Position:" + Input.mousePosition);
    40.        Vector3 mouseDownPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
    41.        Debug.Log("World Position: " + mouseDownPos);
    42.        spline = spriteShapeController.spline;
    43.        int pointCount = spline.GetPointCount();
    44.        int closestPointIndex = int.MaxValue;
    45.        float minDistance = int.MaxValue;
    46.        for (var i = 0; i < pointCount; i++)
    47.        {
    48.            Vector3 currentPointPos = spline.GetPosition(i);
    49.            float distance = Vector3.Distance(currentPointPos, mouseDownPos);
    50.            if (distance < minDistance)
    51.            {
    52.                minDistance = distance;
    53.                closestPointIndex = i;
    54.            }
    55.        }
    56.        spline.InsertPointAt(closestPointIndex, mouseDownPos);
    57.        spline.SetTangentMode(closestPointIndex, ShapeTangentMode.Continuous);
    58.        inseretedPointIndex = closestPointIndex;
    59.        Debug.Log("Inserted point index: " + inseretedPointIndex);
    60.    }
    61.  
    62.    void OnMouseUp()
    63.    {
    64.        Debug.Log("Mouse Up");
    65.        spline = spriteShapeController.spline;
    66.        spline.RemovePointAt(inseretedPointIndex);
    67.        inseretedPointIndex = INVALLID_INSERTED_POINT_INDEX;
    68.    }
    69. }
    70.  

    Basically tried to figure the closest point on the spline where I've clicked and then inserting a new point and setting its position on Update to where the mouse is and delete the point on mouse up.
    Right now I'm having a problem where the drag position is not correct for some reason.
    Where I clicked, where the new point position is set:


    even when I tried to play with where I click and where I take my mouse to while dragging, not sure why, could use help!
     

    Attached Files:

    Last edited: Oct 18, 2019