Hello, I’m somewhat new to Unity. The background of my game is supposed to duplicate itself seamlessly by repositioning itself once it has moved half way along the Z axis (I have a separate script for the movement.) Instead, it just stutters after barely moving. Best I can tell it’s recreating itself almost immediately after moving.
I have another project where I have the exact same script but with the X axis and it works fine, so I’m getting pretty frustrated…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RepeatBackground : MonoBehaviour
{
private Vector3 startPos;
private float repeatWidth;
void Start()
{
startPos = transform.position;
repeatWidth = GetComponent<BoxCollider>().size.z / 2; // Gets half the size of the box collider's Z width
}
void Update()
{
// If the position of the object reaches halfway of its width, then reset to starting position
if (transform.position.z < startPos.z - repeatWidth)
{
transform.position = startPos;
}
}
}
Also, what about scaling? Is the background parented below some other object that is scaled?
Other than those simple checks… time to start debugging!
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
To be honest, I don’t understand most of what you’re describing (I’m only 3 weeks into C#, running through the Junior Programmer pathway.) But you mentioned scale and I noticed that this object’s scale isn’t 1x1x1 and my other project where this code is working does have a scale of 1x1x1.
So I made an empty object (with a scale of 1x1x1) as a parent to my background, and my code is working now!.. Mostly.
The transition isn’t perfectly seamless. I’m having trouble getting the box collider on the empty object the exact same size as the background object. If anyone knows a better way than doing it manually, or if there’s a shortcut, I’d be happy for you to share (I haven’t gone through the Creative Core learning pathway yet, so my skills there aren’t great yet.)