i am going crazy with this problem, it seems that it is really hard for me, and i am asking for help from experienced forum members
in short, i am trying to slide the rectangle that serves as a label to the other side on a certain condition. check out the image:
i guess the problem is because every frame i get to calculate the position of the rectangle (to convert from world to screen space) and
in the same time to slide the rectangle…so i tried to stop calculation when i do sliding, but everything goes back to beginning later, and rect doesnt slide to appropriate place, i guess i should convert from world to screen space again…i dont know it is confusing. i hope all of this makes some sense…
here is the code i have so far…
i made a class that contans information relevant to the label (positions and such…)
here it is:
class CLabel{
function CLabel(width : float,height : float){
rWidth=width;
rHeight=height;
}
var startPoint : Vector3;
var endPoint : Vector3;
var startPoint2D : Vector2;
var endPoint2D : Vector2;
var rPosX : float;
var rPosY : float;
var rWidth : float;
var rHeight : float;
}
in short, what following script does:
everything is done in DrawLabel function
//current camera target transform is stored
target=camScript.currentTarget;
//end and start points for the line are calculated
//based on the position of the target and is it on
//the negative or positive x-axis
CalculateLinePoints();
//calculate position of the rect based on calculated points
//for the line (this is where conversion from world to screen
//space is happening
CalculateRectPoints();
//THIS WILL JUST ENABLE RENDERING INSIDE
//ONPOSTRENDER (maybe we should have to explicitly
//enable rendering in the OnPostRender()
RenderLabelLine();
//renders label rectangle
RenderLabelRect();
i made a script called LabelControl and attached it to the camera:
var cam : Camera;
var camScript : MainCamera;
//MAKE THE INSTANCE OF THE CLABEL OBJECT
var Label : CLabel=new CLabel(200,150);
//VARIABLE THAT HOLDS THE CURRENT TARGET TRANSFORM
var target : Transform;
//ENABLE LINE RENDER
var bEnableRender : boolean = false;
var bCalculatePositions = true;
//MATERIAL THAT WILL BE USED TO RENDER THE LINE
var lineMaterial : Material;
function Awake(){
//GET THE CAMERA INSTANCE
cam=GameObject.Find("MainCamera").GetComponent("Camera");
camScript=GameObject.Find("MainCamera").GetComponent("MainCamera");
target=camScript.currentTarget;
}
function OnGUI(){
if(!(camScript.currentTarget.name=="Center")){
DrawLabel();
}
}
function DrawLabel(){
//GET TARGET
target=camScript.currentTarget;
CalculateLinePoints();
CalculateRectPoints();
//THIS WILL JUST ENABLE RENDERING INSIDE
//ONPOSTRENDER (maybe we should have to explicitly
//enable rendering in the OnPostRender()
RenderLabelLine();
//THIS EXECUTES IN ONGUI ANYHOW
RenderLabelRect();
//MONITOR RECT STATE
//MonitorRectState();
}
function SlideToRight(){
bCalculatePositions=false;
Label.rPosX=Label.endPoint2D.x;
var t = 0.0;
while (t<1.0){
t+=Time.deltaTime *(1.0/0.2);
//MOVE THE RECT
Label.rPosX=Mathf.Lerp(Label.rPosX,Label.rPosX-Label.rWidth,t);
Debug.Log("Label.rPosX: " + Label.rPosX);
yield;
if(t>1.0)
bCalculatePositions=true;
}
}
//THIS FUNCTION JUST ENABLES RENDER LINE
//IN ONPOSTRENDER FUNCTION
function RenderLabelLine(){
bEnableRender=true;
}
//THIS FUNCTION CONSTRUCTS THE RECT
//FROM GIVEN DATA
function RenderLabelRect(){
GUI.BeginGroup(Rect(Label.rPosX,Label.rPosY,Label.rWidth,Label.rHeight));
GUI.Box(Rect(0,0,Label.rWidth,Label.rHeight),"");
GUI.Box(Rect(5,5,Label.rWidth-10,Label.rHeight-10),target.name);
if(GUI.Button(Rect(10,30,40,40),"I")){
SlideToRight();
}
GUI.EndGroup();
}
function CalculateRectPoints(){
var temp : Vector3;
if(bCalculatePositions){
if(Label.endPoint.x>0){
temp=cam.WorldToScreenPoint(Label.endPoint);
Label.rPosX=temp.x;
} else {
temp=cam.WorldToScreenPoint(Label.endPoint);
Label.rPosX=temp.x-Label.rWidth;
}
temp=Camera.main.WorldToScreenPoint(Label.endPoint);
temp.y=Screen.height-temp.y;
Label.rPosY=temp.y-Label.rHeight;
}
}
function CalculateLinePoints(){
//USED TO DETERMINE LEFT OR RIGHT SIDE
//OF THE LABEL
var direction : Vector3;
//IF THE OBJECT IS ON THE RIGHT SIDE
if(target.position.x<0){
direction=Vector3(-1,0.25,0);
} else { //IF THE OBJECT IS ON THE LEFT SIDE
direction=Vector3(1,0.25,0);
}
//STORE THE LINE POSITIONS
Label.startPoint=target.position;
Label.endPoint=direction + Label.startPoint;
Label.startPoint2D=cam.WorldToScreenPoint(Label.startPoint);
Label.endPoint2D=cam.WorldToScreenPoint(Label.endPoint);
}
function OnPostRender(){
if(bEnableRender){
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(Color(1,1,1,1));
GL.Vertex3(Label.startPoint.x,Label.startPoint.y,Label.startPoint.z);
GL.Vertex3(Label.endPoint.x,Label.endPoint.y,Label.endPoint.z);
GL.End();
}
}//END OF ONPOSTRENDER
function CreateLineMaterial() {
if( !lineMaterial ) {
lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }" );
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
} //END OF CREATE MATERIAL FUNCTION