Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nnnachos · Jan 15, 2018 at 03:14 AM · 2dtilemaptiles

Problem with prefabs and offset

Hi, I'm having a simple problem trying to generate a basic 2d tiled map out of 2 prefabs and a player.

Basically, everything seems to have a 0.5 offset so the player can't collide properly with the walls and the floor tiles don't align properly either.

Notice on the screenshot how, with the mouse located at the green dot, the onHover property of the wall is being triggered, and both the player and the floor tiles are not aligning properly (but it seems that the other kind of floor is, but they don't have boxcolliders).

The sprite seems to be 0.5 to the right and 0.5 upwards. The boxcollider property of the wall prefab is set to 0,0,0.

The mouse was located at the green dot.

This is my map generation script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MapManager : MonoBehaviour {
 
     public int width;
     public int height;
 
     public GameObject[] floorTiles;
     public GameObject[] mapBorderTiles;
 
     private Transform mapHolder;
     private List <Vector3> gridPositions = new List<Vector3>();
 
     void BoardSetup(){
         mapHolder = new GameObject ("Map").transform;
 
         for (int x = 0; x < width; x++) {
             for (int y = 0; y < height; y++) {
                 GameObject toInstantiate = floorTiles [Random.Range (0, floorTiles.Length)];
                 if(x == 0 || x == width - 1 || y == 0 || y == height - 1){
                     toInstantiate = mapBorderTiles[Random.Range (0, mapBorderTiles.Length)];
                 }
 
                 GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;
                 instance.transform.SetParent(mapHolder);
 
             }
         }
 
     }
 
     public void setupMap(){
         BoardSetup ();
 
     }
 }

And this are my wall prefab settings.

alt text

EDIT

Ok I'm a complete beginner in Unity but the problem was quite simple. I was treating the position of the player as a point on its sprite's top left vertex and by default it is on the center, so I was always going to be half a sprite off because of my player initialization (int, int, int instead of float, such as 8.5f).

The player now works properly WITHOUT a BoxCollider2d offset, but the offset it is still needed for the wall prefabs.

Is there any way I could further simplify this? Code wise, it is easier to think the prefabs as points on the top-left vertex..

Thanks

wall.png (50.8 kB)
problem.png (12.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by jdean300 · Jan 15, 2018 at 03:23 AM

It seems like you need to check the import settings of the sprite to see where the pivot point of that sprite is located. Failing that, you can change the wall prefab so that what you have currently is under another object and change the position of the child object to have the position of the object located at the center of the tile or wherever it is you need it to be. Check the sprite pivot point settings first.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nnnachos · Jan 15, 2018 at 12:58 PM 0
Share

The pivot doesn't seem to be the problem, see:

alt text

I didn't quite get the other option.

asd.png (70.4 kB)
avatar image nnnachos · Jan 15, 2018 at 01:10 PM 0
Share

I updated the main post with half a solution.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to programmatically switch tile palettes in Unity2D 0 Answers

Confused about ITilemap interface and concrete tilemap classes 0 Answers

TileMap 2D - how to move/animate a tile ? 0 Answers

2D Tiles and sprites 1 Answer

Beginner tutorial for 2D tiles/tilemaps? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges