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 Wabunga · Jun 11, 2016 at 07:11 PM · scalingcollision2dtilinglevel editor

[2D] How do most people make levels with proper collision AND textures?

So, when I put down a series of 1x1 "floor" prefabs with a Box Collider 2d, walking across them is a nightmare. My character will get caught and be unable to move constantly. This could probably be alleviated if I used a sphere collider for my character instead of a box collider, but that would make walking off edges not work like they do in traditional 2d platformers.

Or, I can stretch out a floor prefab and just have one big box collider for a walkable segment using the scale value on the transform. This works great! But then the texture is stretched and looks awful. I don't think I'm supposed to make a new prefab with a custom material for every possible length and height of floors, so how do I make the texture/sprite tile when I scale it? Is there a way to create a new material specific to each gameobject without doing so manually?

I only need one of these two things to work, but since neither do, I'm kind of in a bind. How do most people make levels for a 2d platformer?

Comment
Add comment · Show 4
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 wojtask12 · Jun 12, 2016 at 12:21 AM 1
Share

you can modify the collider size without changing the object's size. just use "Edit Collider" button in inspector

avatar image Wabunga wojtask12 · Jun 12, 2016 at 01:54 AM 0
Share

Yes, but then the size of the collider wouldn't match the size/scaling of the texture. I'd end up with a blurry stretched mess, or a floor that's partially invisible.

avatar image Iplaydev1 · Jun 12, 2016 at 03:03 AM 0
Share

Could you at least put a picture of the problem?

avatar image Wabunga · Jun 12, 2016 at 09:01 AM 0
Share

So I found and slightly edited a script that makes the textures tile by creating instances of the material in the editor as I scale the prefab. This seems to work well, but comes up with an error saying

"Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.shared$$anonymous$$aterial ins$$anonymous$$d."

The script calls "Resources.UnloadUnusedAssets();" after every time it assigns a new instance of the material, will this still cause a memory leak? Or is it fine? Who knows, but I'm using it until Unity releases a tile editor.

Code was found here, and here it is:

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEdit$$anonymous$$ode]
 public class TextureTilingController : $$anonymous$$onoBehaviour {
     
     // Give us the texture so that we can scale proportianally the width according to the height variable below
     // We will grab it from the meshRenderer
     public Texture texture;
     public float textureTo$$anonymous$$eshY = 2f; // Use this to contrain texture to a certain size
     
     Vector3 prevScale = Vector3.one;
     float prevTextureTo$$anonymous$$eshY = -1f;
     
     // Use this for initialization
     void Start () {
         this.prevScale = gameObject.transform.lossyScale;
         this.prevTextureTo$$anonymous$$eshY = this.textureTo$$anonymous$$eshY;
         
         //this.UpdateTiling();
     }
     
     // Update is called once per frame
     void Update () {
         // If something has changed
         if(gameObject.transform.lossyScale != prevScale || !$$anonymous$$athf.Approximately(this.textureTo$$anonymous$$eshY, prevTextureTo$$anonymous$$eshY))
             this.UpdateTiling();
         
         // $$anonymous$$aintain previous state variables
         this.prevScale = gameObject.transform.lossyScale;
         this.prevTextureTo$$anonymous$$eshY = this.textureTo$$anonymous$$eshY;
     }
     
     [Context$$anonymous$$enu("UpdateTiling")]
     void UpdateTiling()
     {
         // A Unity plane is 10 units x 10 units
         float planeSizeX = 10f;
         float planeSizeY = 10f;
         
         // Figure out texture-to-mesh width based on user set texture-to-mesh height
         float textureTo$$anonymous$$eshX = ((float)this.texture.width/this.texture.height)*this.textureTo$$anonymous$$eshY;
         
         gameObject.GetComponent<Renderer>().material.mainTextureScale = new Vector2(planeSizeX*gameObject.transform.lossyScale.x/textureTo$$anonymous$$eshX, planeSizeY*gameObject.transform.lossyScale.y/textureTo$$anonymous$$eshY);
         Resources.UnloadUnusedAssets();
     }
 }


1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Jun 12, 2016 at 07:04 AM

I'd recommend never using the scale transform to create variations of a prefab - you'll inevitably stretch the texture. Instead, you should create separate prefabs for, say 1x1, 2x1, 4x1 blocks that have the correct UV coordinates assigned to their vertices, so you can assign the same material to each and it will tile nicely while also only requiring a single collider per platform.

Comment
Add comment · Show 1 · 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 Wabunga · Jun 12, 2016 at 09:05 AM 0
Share

Walking from one box collider to another causes massive problems the majority of the time, so anything less than the whole floor being one collider is unacceptable for now. I'm going to use the solution I found and wrote about in my comment, but thanks for the suggestion!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Quad Background (Scrolling) Texture = Mutated Image 1 Answer

2D Collider Auto-Tiling for Images? (GUI) 0 Answers

Accidentally did 'something' and now positioning is all out of whack (canvas, camera, scaling?) 0 Answers

Adjusting terrain texture tiling. 1 Answer

Texture is Tied to Two Cubes 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