I have a game where you place beams around. You click on a point, it puts the start of the beam there, then when you click on another point, it sets the end of the beam there.
At the ends of the beams are two spheres, that are added when a beam end is placed. (They are not a part of the beam prefab as the beam is stretched to fit and that stretched the spheres as well.)
I create a new beam with:
beamInProgress = Instantiate(newBeam, new Vector3(), Quaternion.identity);
Then I set its start and end points with:
beamInProgress.GetComponent<NewBeam>().SetStartPoint(placementHighlight.transform.position);
beamInProgress.GetComponent<NewBeam>().SetEndPoint(placementHighlight.transform.position);
Which refers to this:
public class NewBeam : MonoBehaviour
{
Vector3 _top = new Vector3(0,0.5f);
Vector3 _bottom = new Vector3(0, -0.5f);
float scaleFactor = 1/1.42857142857142857f;
public void SetStartPoint(Vector3 start)
{
// NewBeam is scaled to one height.
bottom = start;
}
public void SetEndPoint(Vector3 end)
{
top = end;
}
public Vector3 top
{
get
{
return _top;
}
set
{
transform.localScale = new Vector3(transform.localScale.x,( _top - _bottom).magnitude * scaleFactor, transform.localScale.z);
transform.position = value - ((value - _bottom)/2);
transform.up = _top - _bottom;
_top = value;
}
}
public Vector3 bottom
{
get
{
return _bottom;
}
set
{
transform.localScale = new Vector3(transform.localScale.x, (_top - _bottom).magnitude*scaleFactor, transform.localScale.z);
transform.position = value + ((_top - value) / 2);
transform.up = _top - _bottom;
_bottom = value;
}
}
}
(I have scaleFactor
there because the visible beam is enclosed in a larger invisible beam to make it easier to click.)
As I move, I run beamInProgress.GetComponent<NewBeam>().SetEndPoint(placementHighlight.transform.position);
This way the end of the beam follows the player. Then, when I set the point, I stop calling that and the beam is set.
placementHighlight
is an indicator of where the object will be placed.
Here are the results I’m getting:
Starting the beam:
Extended and placed the beam:
Now, here’s where the problem is happening:
First, the end of the beam is fine:
But, for some reason the start of the beam is off:
The first thing I thought of was
scaleFactor
, but it’s accurate and also it would be evenly off on both sides, so it’s not that.