- Home /
change the length of the vector
hi, i tried to change the length of the vector using Vector3.Scale and it seems that it didnt work, here is the code is used:
//CALCULATE THE END POINT OF THE LINE
MicroLabel[i].endPoint=direction+MicroLabel[i].startPoint;
MicroLabel[i].endPoint=Vector3.Scale(MicroLabel[i].endPoint,Vector3(0.2,0.2,0.2));
it messes up the direction. so the question is how to make vector smaller or bigger in length and not mess the direction in the process? i cant seem to figure that out. also i would like to be somehow connected to the camera.distance so that labels and lines stays inside the viewport..any ideas?
thanks!
Answer by SteveFSP · Nov 06, 2010 at 03:31 PM
Based on the nomenclature you are using (start point, end point), it appears you are scaling the wrong value. Scaling the direction is what you should be doing. Something like:
Vector3 scaledDirection =Vector3.Scale(direction,Vector3(0.2,0.2,0.2));
MicroLabel[i].endPoint=scaledDirection+MicroLabel[i].startPoint;
In general, scaling is used on direction vectors rather than vectors which represent points.
I'm afraid that I don't have complete answer for how to scale a line's length to keep it in the view port. There was a case where I needed to scale the width of a LineRenderer based on the camera's distance from it. In that case, I obtained the distance of the camera from the target, then derived a scale based on that distance. E.g. scaleToApply = distanceToTarget * constantScalingFactor. Rudimentary, but it worked in my case.
Also, to scale a vector uniformly, you can simply multiple by the amount. Vector3 scaledDirection = direction * 0.2;
Your answer
Follow this Question
Related Questions
Change UI scale in relation to 3D camera 0 Answers
Show squares in the camera 0 Answers
Unrealistic size of sun, viewed from earth 2 Answers
Colliders won't work 0 Answers
Camera settings for viewing any model like the Unity Preview 1 Answer