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 /
  • Help Room /
avatar image
0
Question by Kurdle_4855 · Jan 07, 2017 at 12:47 AM · errorcalculation

Calculation Error, please send help.

Hi! So, I'm working on a game in Unity and I am running in to a few issues. I have narrowed this down to error on my end, but I'm not sure what exactly it is. What I'm working on is 2D, block - based terrain, and I am currently doing terrain editing. The problem is, I can't seem to edit blocks if either the X or Y coordinate is below zero. Here's my code:

Vector2 pos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);

         Vector2 cPos = new Vector2(((int)pos.x / World.chunkSize) * World.chunkSize, ((int)pos.y / World.chunkSize) * World.chunkSize);
         Debug.Log(cPos);
         pos.x += 0.5f;
         pos.y += 0.5f;
         Chunk c = Chunk.GetChunkAt((int)cPos.x, (int)cPos.y);
          if(c != null)
         {
             c.RemoveBlock((int)(pos.x - cPos.x), (int)(pos.y - cPos.y));
         }

So basically, I round the mouse position to a Chunk position in world coordinates. Then, I get that chunk and remove the specified block. I have the 'remove block' method to just return if either X or Y is less than zero, X or Y is greater than the chunk size. Otherwise, remove the specified block.

Here's my GetChunk code: public static Chunk GetChunkAt(int x, int y) { foreach(Chunk c in allChunks) { if(c.transform.position.x == x && c.transform.position.y == y) return c; }

     return null;
 }

And my RemoveBlock code:

public void RemoveBlock(int x, int y) { if(x < 0 || y < 0 || x >= World.chunkSize || y >= World.chunkSize) return;

     map[x,y] = 0;
     for(int i = 0; i < transform.childCount; i++)
     {
         if(transform.GetChild(i).transform.position == new Vector3(x,y,0) + this.transform.position && transform.GetChild(i).GetComponent<SpriteRenderer>().sortingOrder == 1){
             Destroy(transform.GetChild(i).gameObject);
             return;
         }
     }
 }

Comment
Add comment · Show 6
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 tanoshimi · Jan 07, 2017 at 07:15 AM 0
Share

"The problem is, I can't seem to edit blocks if either the X or Y coordinate is below zero."

"I have the 'remove block' method to just return if either X or Y is less than zero"

Doesn't the second statement explain the first?

avatar image Kurdle_4855 tanoshimi · Jan 07, 2017 at 04:43 PM 0
Share

Not really, because I convert the position to chunk coordinates (which may not be working properly), then I input it to the function.

avatar image Glurth · Jan 07, 2017 at 06:17 PM 0
Share

Hmm, think we will need to see your RemoveBlock function and chunk class. For example, I don't see why you pass parameters to RemoveBlock.. doesn't the chunk already know where it is? And why do you pass it an offset(subtraction), rather than the actual chunk coordinates used by GetChunkAt?

avatar image Kurdle_4855 Glurth · Jan 07, 2017 at 07:23 PM 0
Share

Well, the 'GetChunk(x,y)' function returns a Chunk at the specified world position. Then, 'RemoveBlock(x,y)' removes the block at the specified LOCAL position of the block. I'll add my code to the question.

avatar image Glurth Kurdle_4855 · Jan 08, 2017 at 05:29 PM 0
Share

hmm, so each chunk position defines the corner of a chunk (blockX,blockY both > 0) the number of possible blocks in a chunk is World.chunkSize^2 (squared), right?

AH! Can I ask you to confirm, with some debug logs, that this line is actually doing what you expect: Vector2 pos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);

I think you are passing an incorrect parameter. The Vector2 mouse position, is converted into a Vector3, with a Z-component of Zero. https://docs.unity3d.com/ScriptReference/Vector3-ctor.html So, when the ScreenToWorld function computes the world point, is uses a distance from the camera of Zero: which results in the camera itself's world position. https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

I think you will need to compute the distance, along the camera view angle (use the vector camera.transform.forward), to the plane you want (plane: z=0). (lots of stuff out there for that: here's my "I'm feeling lucky" result- http://mathinsight.org/distance_point_plane)

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

78 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

Related Questions

Compiler Error, Cannot Go Into Playmode 2 Answers

HELP!! unexpected token: If 1 Answer

Error CS8025: Parsing Error 3 Answers

Unity 5.2.1 not responding after hitting Play 2 Answers

Setting EventTrigger in Unity with JavaScript 0 Answers


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