Search Unity

Movable object issue

Discussion in 'Scripting' started by Thade2k, Feb 16, 2018.

  1. Thade2k

    Thade2k

    Joined:
    Sep 15, 2016
    Posts:
    9
    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:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class ObjectMover : MonoBehaviour
    7. {
    8.     public Vector3 m_direction;
    9.     public float m_distance;
    10.     public float m_speed;
    11.  
    12.     [HideInInspector]
    13.     public Vector3 m_velocity;
    14.  
    15.     private Vector3 m_initialPos;
    16.     private Vector3 m_targetPos;
    17.     private bool m_isReturning;
    18.     private float m_duration;
    19.     private float m_timer;
    20.  
    21.  
    22.     private void Start()
    23.     {
    24.         m_isReturning = false;
    25.         m_timer = 0f;
    26.         m_duration = m_distance / m_speed;
    27.  
    28.         m_direction.Normalize();
    29.         m_initialPos = transform.position;
    30.         m_targetPos = transform.position + m_direction * m_distance;
    31.     }
    32.  
    33.     private void Update()
    34.     {
    35.         m_timer += Time.deltaTime;
    36.         Vector3 lastPos = transform.position;
    37.  
    38.         if (m_isReturning)
    39.         {
    40.             if (m_timer < m_duration)
    41.             {
    42.                 float lerpFactor = Mathf.Clamp01(m_timer / m_duration);
    43.                 transform.position = Vector3.Lerp(m_targetPos, m_initialPos, lerpFactor);
    44.  
    45.                 m_velocity = transform.position - lastPos;
    46.             }
    47.             else
    48.             {
    49.                 m_isReturning = false;
    50.                 m_timer = 0f;
    51.             }
    52.         }
    53.         else
    54.         {
    55.             if (m_timer < m_duration)
    56.             {
    57.                 float lerpFactor = Mathf.Clamp01(m_timer / m_duration);
    58.                 transform.position = Vector3.Lerp(m_initialPos, m_targetPos, lerpFactor);
    59.  
    60.                 m_velocity = transform.position - lastPos;
    61.             }
    62.             else
    63.             {
    64.                 m_isReturning = true;
    65.                 m_timer = 0f;
    66.             }
    67.         }
    68.     }
    69. }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It kind of sounds like you parented to the first object and didn't unparent, but there's no code for it in there.