[SCRIPTING]Moving sky sprite with script sometimes staggers
Greetings, I'm assembling some moving sky using a script. I have a master SkyGO, under that I have a ChildSkyGO, under that my tileable sprites. Here is my code:
public class MovingSky : MonoBehaviour
{
public float skyScrollSpeed = 10.0f;
public float moveLimit = 68.58f;
public float damping = 1.0f;
private float deltaMovement = 0.0f;
private Vector3 idleLocalPos;
Vector3 currentVelocity;
Vector3 target;
Vector3 newPos;
void Start ()
{
idleLocalPos = transform.localPosition;
}
// Update is called once per frame
void Update ()
{
if (!CharacterController2D.movementDisabled)
{
target = new Vector3(transform.localPosition.x - skyScrollSpeed,
transform.localPosition.y, idleLocalPos.z);
newPos = Vector3.SmoothDamp(transform.localPosition, target, ref currentVelocity, damping);
//transform.localPosition = new Vector3(transform.localPosition.x - skyScrollSpeed,
// transform.localPosition.y, transform.localPosition.z);
transform.localPosition = newPos;
deltaMovement += skyScrollSpeed;
}
if(Mathf.Abs(transform.localPosition.x) >= moveLimit)
{
transform.localPosition = Vector3.zero;
deltaMovement = 0.0f;
}
}
}
The reason for the use of localPosition is because the sky has to follow the moving player not to get off screen. IMPORTANT: The staggering does NOT happen when I pop the sky back. However it happens at a regular basis. All the help appreciated. Edit: of course at the beginning the character movement is never disabled. I tried getting this out of the if, it didn't work.
Your answer
Follow this Question
Related Questions
While loop won't work, it freezes Unity 1 Answer
How would i modify my current script to remove these UI buttons without killing my pause menu? 1 Answer
Why does NetworkServer.ReplacePlayerForConnection not recognize the instantiated object parameter? 0 Answers
How to switch between two lightmaps in unity 5 1 Answer
IEnumerator, Unexpected symbol? 3 Answers