Hi guys, I made a script for a movable object that’s attached to a prefab and I can control the all the axis, speed and distance. My problem is this that I have 5 similar objects, I jump on the first one, everything works great, the player moves with the object, it runs smooth, after I jump on the second object, the player character can barely move, he’s shaking a lot and I can’t figure out why. Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectMover : MonoBehaviour
{
public Vector3 m_direction;
public float m_distance;
public float m_speed;
[HideInInspector]
public Vector3 m_velocity;
private Vector3 m_initialPos;
private Vector3 m_targetPos;
private bool m_isReturning;
private float m_duration;
private float m_timer;
private void Start()
{
m_isReturning = false;
m_timer = 0f;
m_duration = m_distance / m_speed;
m_direction.Normalize();
m_initialPos = transform.position;
m_targetPos = transform.position + m_direction * m_distance;
}
private void Update()
{
m_timer += Time.deltaTime;
Vector3 lastPos = transform.position;
if (m_isReturning)
{
if (m_timer < m_duration)
{
float lerpFactor = Mathf.Clamp01(m_timer / m_duration);
transform.position = Vector3.Lerp(m_targetPos, m_initialPos, lerpFactor);
m_velocity = transform.position - lastPos;
}
else
{
m_isReturning = false;
m_timer = 0f;
}
}
else
{
if (m_timer < m_duration)
{
float lerpFactor = Mathf.Clamp01(m_timer / m_duration);
transform.position = Vector3.Lerp(m_initialPos, m_targetPos, lerpFactor);
m_velocity = transform.position - lastPos;
}
else
{
m_isReturning = true;
m_timer = 0f;
}
}
}
}