#Unity Scale Calculations... Fail. Help?

1 messages Β· Page 1 of 1 (latest)

deep dagger
#

Does anyone know why using Scale is just... being weird?

Basically, I use two arrows to change the scale OR position of a target. It depends on what mode I'm currently in. The axis I use are X and Y.
I also use 1 ring on the Z axis to change the rotation of the target.
This is the scale function, which works correctly if I haven't changed the rotation of the target:

void EditScale(Ray camRay, float planeDist)
{
    // Calculate the change in mouse position
    Vector3 currentMousePosition = camRay.GetPoint(planeDist);
    float delta = currentMousePosition.y - initial.y; // Use the y component for vertical movement

    // Calculate the scale factor based on the mouse movement
    float scaleFactor = 1f + delta * ScaleMultiplier; // You can adjust the multiplier based on your preference

    // Apply the scale based on the axis
    if (axis == Axis.X)
        Thing.transform.localScale = new Vector3(initial.x * scaleFactor, Thing.transform.localScale.y, Thing.transform.localScale.z);
    else if (axis == Axis.Y)
        Thing.transform.localScale = new Vector3(Thing.transform.localScale.x, initial.y * scaleFactor, Thing.transform.localScale.z);
    else if (axis == Axis.Z)
        Thing.transform.localScale = new Vector3(Thing.transform.localScale.x, Thing.transform.localScale.y, initial.z * scaleFactor);

    // Update the initial position for the next frame
    initial = currentMousePosition;
}```
#

However, if i rotate it, using this function:

void EditRotation(Ray camRay, float planeDist)
{
    // Calculate the change in mouse position
    Vector3 currentMousePosition = camRay.GetPoint(planeDist);

    // Calculate the rotation angle based on the change in mouse position
    Vector3 oldDelta = initial - Thing.transform.position;
    Vector3 newDelta = currentMousePosition - Thing.transform.position;
    float delta = Vector3.SignedAngle(oldDelta, newDelta, Vector3.forward);

    // Apply the rotation based on the axis
    if (axis == Axis.Z)
    {
        Thing.transform.Rotate(Vector3.forward, delta);
    }

    // Update the initial position for the next frame
    initial = currentMousePosition;
}```

Then, it just stops working. After rotating, if i try to drag an arrow, it just goes to the opposite side of the object. Here's the visualized issue: