2D Orthographic Cameras and Line/Trail Renderers Not Working (with detailed example)
So there are tons of questions out there about orthographic cameras + LineRenderer or TrailRenderer not working together or working strangely together, and I think I've read a hundred of them. Still - I can't get them to work together.
I've laid out an extremely simple example using TrailRenderer instead of LineRenderer for simplicity, but they share the same problem. The basic issue is that although I can see my Lines/Trails in the Scene view, they don't show in the Game view.
The Example:
Created an empty GameObject called Unit
Added a Sprite object called UnitSprite as a child of Unit (Holding the Unit Sprite) http://i.imgur.com/0zczDhj.png
Added a Camera Component on Unit and made it Orthographic http://i.imgur.com/86t7xuN.png
Added a TrailRenderer Component on Unit http://i.imgur.com/InSAtME.png
Added a simple movement script to Unit. (included below)
Simple Movement Script:
public class SimpleMovement : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
int horizontal = 0;
int vertical = 0;
horizontal = (int)(Input.GetAxisRaw ("Horizontal"));
vertical = (int)(Input.GetAxisRaw ("Vertical"));
Vector2 start = transform.position;
Vector2 end = start + new Vector2 (horizontal, vertical);
Vector2 newPosition = Vector2.MoveTowards (start, end, .1f);
transform.position = newPosition;
}
}
Things I've tried:
Playing with every permutation of Sorting Layer and Z-values
Camera.transparencySortMode and Camera.opaqueSortMode
Using a different Orthographic camera than one attached to Unit
Madly changed every value I could find in every possible way all the while laughing hysterically
I'm hoping that if I can solve this problem with the TrailRenderer I'll be able to fix my real problems with LineRenderers + TrailRenderers in my actual game. Any help is super appreciated, so thanks in advance.
Happy Holidays.
Answer by abeering · Dec 22, 2015 at 12:40 AM
Okay so the answer was in SortingLayers. The most important bit I noticed is that no matter what the parent GameObject is set to, I still needed to set the SortingOrderName for the TrailRenderer component in the script, otherwise it would just always render behind every other sprite regardless of Sorting Layer or Z value.
Your answer
Follow this Question
Related Questions
Can't get a smooth following line 1 Answer
y line render getting norrow at the end 1 Answer
Moving dotted guide line 0 Answers
Unity Trail Renderer Shows in Unity Editor but not in-game. 0 Answers
How to make game like Line 1 Answer