- Home /
Moving object disappears then comes back
Hey, so I've got a script where a 2D obstacle is supposed to move up and down on a loop. The script works fine, but for a second or two, the obstacle just goes invisible and then comes back. However, it only goes invisible in the game area, not in the scene area. I'm going to attach my code and a video link to show whats happening. Here's the video: https://vimeo.com/282669279. Here's the code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpikeMovement : MonoBehaviour {
public Transform farEnd;
private Vector3 frometh;
private Vector3 untoeth;
public float secondsForOneLength = 20f;
// Use this for initialization
void Start () {
frometh = transform.position;
untoeth = farEnd.position;
}
// Update is called once per frame
void Update () {
transform.position = Vector3.Lerp(frometh, untoeth,
Mathf.SmoothStep(0f, 1f,
Mathf.PingPong(Time.time / secondsForOneLength, 1f)
));
}
}
Hi, are you sure that there aren’t objects with z values out from your camera view?
I just checked and that seemed to be the problem, it's fixed now thanks. But the weird thing is, all the Z values were the same when I first checked, but when I went into 3D mode it was different. I don't know if that's a bug or something but it's working now. The Z values are different now however.
Answer by Bubinga_Studios · Aug 01, 2018 at 03:44 PM
It seems like your script is written out and working correctly, however...
The issue is probbably that it is lerping through the Z axis, making it go behind and in front of the background.
Make sure that you are using a 2D solution for this. Replace your Vector3.Lerp with a Vector2.Lerp instead.
Hope this helps you out! If not, let me know.