Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Aug 18, 2013 at 07:37 PM by WizzDE for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by WizzDE · Aug 17, 2013 at 03:08 PM · bugvector2unity 4.2zero

Vector2 is always (0,0) - HELP

Hey, I'm working on a voxel engine and now I'm trying to set up a dynamic block texture uv system (just calculating the uv coords in anothe script). The problem is that when I define the Vector2 with the calculated variables it's still zero ( or (0,)) Here is some code:

 private function ApplyTexture(x, y, z){
     var xx : float;
     var yy : float;
 
     switch (voxelContainer[Vector3(x,y,z)]){
         case 1:

             xx = blocks.GetBlockCoordX(1, 1, 0);
             yy = blocks.GetBlockCoordY(1, 1, 0);
             uvs.Add(Vector2(xx, yy)); //This is always (0,0) but why?
             if(p){
                 print(xx); //This prints the right value which is 0.25
                 p = false;    
             }
             xx = blocks.GetBlockCoordX(0, 1, 0);
             yy = blocks.GetBlockCoordY(0, 1, 0);
             uvs.Add(Vector2(xx, yy));            //Same for the rest...
             xx = blocks.GetBlockCoordX(0, 0, 1);
             yy = blocks.GetBlockCoordY(0, 0, 1);
             uvs.Add(Vector2(xx, yy));
             xx = blocks.GetBlockCoordX(0, 0, 0);
             yy = blocks.GetBlockCoordY(0, 0, 0);
             uvs.Add(Vector2(xx, yy));           //...
 
             break;

This works because if I'm putting in the right values (0.25, 0.75 etc.) everything works fine but as soon as I use the variables it's messed up. Here is the GetBlockCoord() function:

 import System.Collections.Generic;
 
 var coords : List.<Vector2>; //I edit this in the inspector ...

 
 var texRes : float = 16;
 
 var blockWidht : float = 4;
 var blockHeight : float = 4; //...
 
 //For the X value...
 public function GetBlockCoordX(xOffset : float, yOffset : float, type : float) : float{
     var factor : float = 1 / texRes;
     var x : float;
     var y : float;
     x = (blockWidht*(coords[type].x)+xOffset*blockWidht) * factor;   //Well a bit messy but I get
     y = (blockHeight*(coords[type].x)+yOffset*blockHeight) * factor; //the right value from it.
     
     return x;
     
 }
 //For the Y value...
 public function GetBlockCoordY(xOffset : float, yOffset : float, type : float) : float{
     var factor : float = 1 / texRes;
     var x : float;
     var y : float;
     x = (blockWidht*(coords[type].x)+xOffset*blockWidht) * factor;
     y = (blockHeight*(coords[type].x)+yOffset*blockHeight) * factor;
     
     return y;
     
 }

What is wrong? Or is this some sort of bug? Help would be awesome! (Sorry for my bad english :P) Wizz

Comment
Add comment · Show 8
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 JoaquinRD · Aug 17, 2013 at 03:17 PM 0
Share

I'm not sure if this will fix your problem, but I don't think that you can index an array or list with a float. Try changing

 type : float

in your GetBlockCoordX and GetBlockCoordY functions to

 type : int

A cleaner solution would be to define an enumeration and use that ins$$anonymous$$d of a number.

avatar image ArkaneX · Aug 17, 2013 at 03:47 PM 0
Share

If xx is properly printed as 0.25, then could you check what is assigned to yy?

And apart from checking the above, in your code you have:

 coords[type].x

in both x and y assignments. Is it ok?

avatar image ArkaneX · Aug 17, 2013 at 03:48 PM 0
Share

And a bit of advice: to optimize the code, you can create one function, returning Vector2, ins$$anonymous$$d of having two functions with almost the same code.

avatar image WizzDE · Aug 17, 2013 at 04:58 PM 0
Share

I know but I had the same problem with this so I changed it

avatar image ArkaneX · Aug 17, 2013 at 05:06 PM 0
Share

And what about my previous comment? Have you checked yy and coords[type].x?

Show more comments

1 Reply

  • Sort: 
avatar image
1

Answer by WizzDE · Aug 18, 2013 at 03:38 PM

Great News! I fixed it! I should have thought about it first...Basicly a the Point (0,0) in UV space is in the bottom left corner. Well I calculated the x which was right but the y was wrong because it is calculated from the top left corner. So my y was basicly the same as my x which Ends up in having black blocks in my engine -.-

So thanks for the Answers!

Wizz

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Why does the code stops? 3 Answers

Collision issues with Vector2.Reflect 1 Answer

Unity 4.2 - adding to a Vector2 is broken??? 1 Answer

Can't change Material Color in Unity 4.2? 1 Answer

Unity3D 4.2.0 Crasing ... !!! 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