So i am trying to create a script which will allow the player to click on a game object and drag out to stretch it or drag in to make it smaller. The object in question is a circle.
The problem I think I am having is that I am struggling to find the distance between the current mouse position and the initial start point of the dragging. In the inspector, they are showing up as the same. Can anyone advise me on how to fix my script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StretchShape : MonoBehaviour
{
public bool dragging = false;
public Vector3 dragStartPoint;
public Vector3 currentMousePosition;
public Vector3 temp;
public float amountDragged = 0.0f;
private void OnMouseDown()
{
dragStartPoint = (Input.mousePosition);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
//If not dragging:
if (dragging == false)
{ //Run raycast on mouse position.
Vector2 rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(rayPos, Vector2.zero, 0f);
//If raycast hit something:
if (hit)
{
//If raycast hit object:
if (hit.collider != null)
//Store current mouse position
{
currentMousePosition = (Input.mousePosition);
//Set dragging = true
dragging = true;
}
}
}
//Else if we are dragging:
else if (dragging == true)
//Calculate distance to drag start point
{
amountDragged = Vector3.Distance(currentMousePosition, dragStartPoint);
//Scale accordingly (transform)
temp = transform.localScale;
temp.x += amountDragged;
temp.y += amountDragged;
}
}
else
// reset dragging
{
dragging = false;
}
}
}