Unity not Working Like in 2D Tutorials

In the Top Down Basics 2D tutorial he just grabs a set of sprites to create an animation and drops it into the game, a game object is automatically added. I did this. It was invisible. I then changed 1 attribute and I can see the sprite now, but it’s extremely close to the camera, this is not okay. Please help if you have extra time!

Thank you!
Have a nice day!

Okay, so if I scale the image 0.1 down to 10% it looks right, but disappears if I zoom in too much.

I must have my depths all messed up…

Grid → Tilemap1 (Z=50) → Tilemap2 (Z=48) …

Samurai Z = 21 Scale = 0.1 orderInLayer = 10

Does this help?!

This seems like such a simple question, could I provide more detail for you all? Is there something I have left out that would help you in understanding how to help me with this issue?

At what position is your camera in z? I’m not fully understanding what you’re changing, but if you’re changing the z-position of any objects, they will stop being drawn if the cross your camera’s near plane. I think there are also problems caused by setting the grid as parent and changing z-positions and scales.

Start by changing the z-scale of everything to 1. You don’t need to change it since it’s a 2D game.

You can try dragging the character outside of the room and back in to see if it’s going behind other graphics. If it’s still not visible when it’s outside the room, draw order is not the issue.

What is the sorting for the other objects? If possible, can you make a gif of what’s happening? What is the “attribute” you changed in your first post?

A side note for you: Don’t get discouraged if you don’t get replies right away. We’re on lots of different time zones on this forum, and it’s not totally unusual to see a 2 or 3 day wait for replies for various reasons. I do think you could have added a bit more detail in your question, but the pictures were a great idea.

1 Like

Check your sprite’s import settings. Do you need to set your Pixels Per Unit?

You can probably keep all your objects at position Z=0 and use SortingGroup, SortingLayer, SortingOrder, and/or Transparency Sort Axis to control the order your sprites render.

2 Likes

Thank you! I will try this, this all seems viable. SortingGroup, SortingLayer, and SortingOrder all seems useful to help resolve my issues!

Thank you! I really appreciate this advice. I am going to use this to resolve my issues with this game!

Okay, so I fixed the issue that caused the scale of the sprite to be absurdly huge. That was changing the pixels per unit on the sprite itself from what I had 1 to 16. 1 cause it to be huge. 16 caused it to be normal, I think…

I tried uploading a .zip of the project so you guys can check it out. But I think some systems strip essential parts of the zip file out to protect users against viruses. Anyways… if you need details, even a lot, to help you answer some of my questions I would provide those details.

One user mentioned that the tilemaps maybe should not be children of the grid, well by definition in one of the tutorials they said tilemaps will always, and have to be, children of the grid.

3389314–266444–realization.zip (2.54 MB)

I didn’t say that I don’t think they should be children.

I meant that if you’re changing z-positions and scales within a parenting structure, you could have problems with clipping due to unexpected z-positions. I still think you should set all of your z-scales to 0. EDIT: I meant to say 1, not 0

1 pixel per unit is a pretty large setting for a lot of cases, but is a popular choice for “pixel-perfect” graphics. Is this what you’re going for? What are your other sprites’ sizes set to in their import settings?

How does the pixels per unit setting even affect the game?

It’s how many pixels of your sprite are equal to 1 unity unit. So if you move your sprite from position (0,0) to (1,0), that sprite would have moved PixelsPerUnit amount of pixels to the right.

Decide what your basic unit of measurement is in pixels, this is generally your “tile” size.

Does this affect gravity and speed? I ask because gravity is defaulted to -9.81 acceleration. I already know that we cannot use gravity in a top down Zelda style game, but if PPU is set to 16 then the accelleration is (16 * gravity) pixels?

Gravity is -9.8 units/s, so your objects using gravity will always travel that speed.

If your object is 1 PPU, it will be moving -9.8 pixels/s (but your pixels are the size of 1 unit). Two objects of differing PPU will fall at the same rate in units, but the one with higher PPU will travel farther in terms of its own pixels.

Since -9.8/s is intended for the default 100 PPU, if you wanted to change the gravity to scale with your new PPU, you can do this:

(DefaultPPU / NewPPU) * DefaultGravity = NewGravity

Interesting! Food for thought!

TY!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Entity : MonoBehaviour {
    public float speed;
    void Start () {};
    void Update () {};}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : Entity {
   
    void Start () {
       
    }
   
    void Update () {
        if(Input.GetKey(KeyCode.W)) {
            GetComponent<Rigidbody2D>().transform.position += Vector3.up * speed * Time.deltaTime;
        }
       
        if(Input.GetKey(KeyCode.A)) {
            GetComponent<Rigidbody2D>().transform.position += Vector3.left * speed * Time.deltaTime;
        }
       
        if(Input.GetKey(KeyCode.S)) {
            GetComponent<Rigidbody2D>().transform.position += Vector3.down * speed * Time.deltaTime;
        }
       
        if(Input.GetKey(KeyCode.D)) {
            GetComponent<Rigidbody2D>().transform.position += Vector3.right * speed * Time.deltaTime;
        }
    }
       
}

Hello! So This is a script I have to move a sprite around the screen. What I want to do is have a four directional top down Zelda style RPG. The problems with the above script is as follows:

I don’t think transform.position is appropriate… I was reading the tutorials and I think that using 2D physics would be better…

How can I have a 3 frame animation for each direction stop on the mid frame of the direction when releasing a key? If this is simple, I can learn quickly. I am looking this up so I don’t expect you to spoon feed me, but I thought maybe you guys may know how to do this very quickly.

Thank you! Have a nice day!

I still think 2d transform doesn’t work in my situation. Would anyone else have any ideas?

I identify different problems with your scripts:

  • If you must subclass a custom MonoBehaviour, be very careful about implementing the magic methods (like Start and Update) in both places. They won’t both get called. To avoid confusion, I strongly recommend you delete your empty Start and Update methods in the Entity class (if indeed you need an Entity class at all).
  • GetComponent().transform belies a misunderstanding of what these things do. Let me try to explain: a GameObject has a number of components, including a Transform, and any MonoBehaviour you put on it. When you call GetComponent on yourself, you’re looking up another component on the same GameObject as this script. When you use .transform on any component, you’re really looking up the Transform component on the same game object as that one. So… you just looked up the Rigidbody2D and then turned around and used it to get the Transform on yourself. Don’t do that. Just use transform directly.
  • You are still laboring under the belief that you want to use physics for this. You don’t. Physics here will lead to wailing and gnashing of teeth. (And I guarantee you that the Zelda games you’re inspired by did not have any physics engine under the hood.)
  • You really shouldn’t be looking for specific keys, but instead be using Input.GetAxis, which may happen to map to certain keys in the Input Manager settings.

EDIT: This article might have a lot of good info for you. Be sure to download the code/project, as some of the code got a little messed up when publishing to Gamasutra.

1 Like

Thank you for this information.

  • Excellent point, also one I would have not known. I am going to eliminate the unnecessary inheritance. If the parent class Entity has a Health Variable, and there’s two children Player and Enemy, then do they have unique healths? Thank you!
  • Yeah, definitely no need to compound the overhead of running the game. I will use transform directly to increase performance. Thank you for pointing this out.
  • I wont use physics, I promise! I appreciate this information you have given to me, in both threads, and I now have a true understanding of the reason that transform truly fits this.
  • I understand what this means now. I’ve seen in tutorials to set the movement to get axis, but I didn’t understand the underlying reasoning to do this, and it’s built in button mapping! I was under the assumption I would have eventually changed that to have variables instead of specific keys, and this was going to be to create my own button mapping.

Thank you JoeStrout!

1 Like

I’m not sure quite what you mean by that. The parent class field will be inherited by both child classes as you would expect.

The only special thing about the magic methods (Start, Update, etc.) is that Unity looks them up by name, and it stops as soon as it finds a match. It doesn’t continue up the inheritance chain looking for additional methods. So this can trip you up if, for example, you expect the base class Update to fire, and then want to specialize it by also having an Update method in the subclass — that doesn’t work. Of course once you understand this you can work around it in various ways.

But the general design pattern encouraged by Unity is one of composition (lots of little component scripts that are combined into one GameObject as needed) rather than inheritance. Your life will generally be easier if you embrace this.

Just FYI, you can mark your Unity methods as virtual and they can be properly overridden in subclasses.