- Home /
The question is answered, right answer was accepted
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
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.
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?
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.
I know but I had the same problem with this so I changed it
And what about my previous comment? Have you checked yy and coords[type].x?
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
Follow this Question
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