U
U
Uncle Bogdan2021-09-06 20:50:20
Unity
Uncle Bogdan, 2021-09-06 20:50:20

Why is Lerp not working correctly?

I wanted to make a door opening system. I decided to create a _openingTime field to store the time for which the door will open.

Decided to use the Lerp method. At first I thought that everything was working, but the door opens + - in a second and does not depend on the value. How to fix it?

The code:

using System.Collections;
using UnityEngine;
using UnityEngine.Events;

public class Door : MonoBehaviour
{
    [SerializeField] private Vector3 _rotationOffset;
    [SerializeField] private Vector3 _positionOffset;

    [SerializeField] private float _openingTime = 2f;

    [Space]

    [SerializeField] private AudioSource _openSound;
    [SerializeField] private AudioSource _closeSound;

    [SerializeField] private Transform _root;

    [Space]

    [SerializeField] private bool _justOneDiscovery;
    [SerializeField] private bool _firstOpen;

    [SerializeField] private bool _open;

    [Space]

    [SerializeField] private UnityEvent _actionEvents;


    private Transform _movingPart;

    private Vector3 _newRotation;
    private Vector3 _newPosition;

    private bool _working;

    private void Awake()
    {
        if (_root != false)
        {
            _movingPart = _root;
        }
        else
        {
            _movingPart = transform;
        }
    }

    private void Update()
    {
        if(_working)
        {
//LERP
            _movingPart.position = Vector3.Lerp(_root.position, _newPosition, _openingTime * Time.deltaTime);
            _movingPart.eulerAngles = Vector3.Lerp(_root.eulerAngles, _newRotation, _openingTime * Time.deltaTime);
        }
    }

    private void OnMouseDown()
    {
        if(!_working)
        {
            if (_justOneDiscovery && _firstOpen || !_justOneDiscovery)
            {
                if (_open)
                {
                    Close();
                }
                else
                {
                    Open();
                }

                StartCoroutine("StopWorking");
            }
        }
    }

    private void Open()
    {
        _newRotation = _movingPart.eulerAngles + _rotationOffset;
        _newPosition = _movingPart.position + _positionOffset;

        if(_openSound != null)
        {
            _openSound.Play();
        }

        if(_actionEvents != null)
        {
            _actionEvents?.Invoke();
        }

        _firstOpen = true;

        _working = true;
    }

    private void Close()
    {
        _newRotation = _movingPart.eulerAngles - _rotationOffset;
        _newPosition = _movingPart.position - _positionOffset;

        if (_openSound != null)
        {
            _closeSound.Play();
        }

        _firstOpen = true;

        _working = true;
    }

    private IEnumerator StopWorking()
    {
        yield return new WaitForSeconds(_openingTime);

        _working = false;

        _open = !_open;

        _newRotation = Vector3.zero;
        _newPosition = Vector3.zero;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-09-06
@motkot

Yes, finally read what the lerp does and what the third argument means to it! It behaves absolutely in accordance with the documentation.
Formula hint (roughly speaking):
min + (max-min)*third_lerp_argument

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question