unity – Find out how to keep away from from the targetCamera to “bounce” to the brand new place every time when altering the ui slider worth?

unity – Find out how to keep away from from the targetCamera to “bounce” to the brand new place every time when altering the ui slider worth?

[ad_1]

The issue is that I do not need the digital camera to maneuver and rotate with the remodel object when rotating the remodel object. that’s the reason i added the if verify within the LateUpdate:

if (rotateDice.isRotating == false)

however now as a result of I added this if verify now after I change the slider worth the targetCamera is “leaping” to the brand new place earlier than zooming out or in as a substitute lerp easily to the brand new place. if I’ll take away the if verify line within the LateUpdate then the targetCamera will easily lerp to the brand new place every time altering the slider worth bit the targetCamera will even get affected from the RotateDice script when rotating the remodel with the mouse.

utilizing UnityEngine;
utilizing UnityEngine.UI;
utilizing UnityEngine.EventSystems;
utilizing Unity.VisualScripting;
utilizing System.Collections;

public class CameraZoomSlider : MonoBehaviour
{
    public Digital camera targetCamera; // Assign this within the inspector
    public Slider zoomSlider; // Assign this within the inspector
    public float minDistance = 5f; // Minimal distance from the item
    public float maxDistance = 15f; // Most distance from the item
    public RotateDice rotateDice;
    public float smoothSpeed = 2f; // Pace at which the digital camera zooms in/out

    personal Vector3 initialDirection;
    personal float distanceToMove;

    void Begin()
    {
        if (targetCamera == null || zoomSlider == null || rotateDice == null)
        {
            Debug.LogError("CameraZoomSlider requires references to the digital camera, slider, and rotateDice.");
            return;
        }

        // Calculate preliminary route and distance from the goal object
        initialDirection = (targetCamera.remodel.place - remodel.place).normalized;
        distanceToMove = (targetCamera.remodel.place - remodel.place).magnitude;

        // Setup the slider
        zoomSlider.minValue = minDistance;
        zoomSlider.maxValue = maxDistance;
        zoomSlider.worth = distanceToMove;
        zoomSlider.onValueChanged.AddListener(OnSliderValueChanged);

        // Add EventTrigger part to the slider if it would not exist
        EventTrigger set off = zoomSlider.gameObject.GetComponent<EventTrigger>() ?? zoomSlider.gameObject.AddComponent<EventTrigger>();

        // Create PointerDown entry
        EventTrigger.Entry pointerDownEntry = new EventTrigger.Entry();
        pointerDownEntry.eventID = EventTriggerType.PointerDown;
        pointerDownEntry.callback.AddListener((knowledge) => { OnPointerDown((PointerEventData)knowledge); });
        set off.triggers.Add(pointerDownEntry);

        // Create PointerUp entry
        EventTrigger.Entry pointerUpEntry = new EventTrigger.Entry();
        pointerUpEntry.eventID = EventTriggerType.PointerUp;
        pointerUpEntry.callback.AddListener((knowledge) => { OnPointerUp((PointerEventData)knowledge); });
        set off.triggers.Add(pointerUpEntry);
    }

    personal void OnSliderValueChanged(float distance)
    {
        distanceToMove = distance;
    }

    personal void LateUpdate()
    {
        if (rotateDice.isRotating == false)
        {
            // Calculate the brand new goal place
            Vector3 targetPosition = remodel.place + initialDirection * distanceToMove;

            // Easily interpolate to the goal place
            targetCamera.remodel.place = Vector3.Lerp(targetCamera.remodel.place, targetPosition, smoothSpeed * Time.deltaTime);

            // Be certain that the digital camera remains to be trying on the object
            targetCamera.remodel.LookAt(remodel.place);
        }
    }

    personal void OnPointerDown(PointerEventData knowledge)
    {
        rotateDice.isRotating = false;
    }

    personal void OnPointerUp(PointerEventData knowledge)
    {
        rotateDice.isRotating = true;
    }
}

and the RotateDice script

utilizing UnityEngine;

public class RotateDice : MonoBehaviour
{
    public float torque = 50.0f;
    public Rigidbody rb;
    public bool isRotating = true;

    void Begin()
    {
        rb = GetComponent<Rigidbody>();
    }

    void OnMouseDrag()
    {
        if ((isRotating))
        {
            rb.AddTorque(Vector3.up * torque * -Enter.GetAxis("Mouse X"));
            rb.AddTorque(Vector3.proper * torque * Enter.GetAxis("Mouse Y"));
        }
    }
}

each scripts hooked up to the identical remodel.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply