[ad_1]
I’m utilizing x and z axis to go left/proper ; up/down respectively. The Camera is on the y-axis.
I’ve the next code to this point –
non-public float playerWidth;
non-public float playerDepth;
non-public Vector2 screenBounds;
// Other Variables and Initiators
// Start known as earlier than the primary body replace
void Start()
{
participantRigidbody = GetComponent<Rigidbody>();
screenBounds = Camera.most important.ScreenToWorldPoint(new Vector3(Screen.width, Screen.peak, Camera.most important.remodel.place.z));
//offers x and y values that might be half the display screen worth. Screen coordinate system is reversed, so these might be adverse values
playerWidth = remodel.GetComponent<BoxCollider>().bounds.measurement.x / 2;
playerDepth = remodel.GetComponent<BoxCollider>().bounds.measurement.z / 2;
// we want half the scale as we already are on the heart of the article, utilizing clamp
}
Tutorials are utilizing SpriteRenderer as this is able to make extra sense in a 2D sport, nonetheless I’m working in 3D house, making a 2.5D impact.
non-public void LateUpdate()
{
insideScreenBounds();
}
non-public void insideScreenBounds()
{
// test if participant doesn't exceed digital camera view, else cease all motion.
Vector3 viewPos = remodel.place;
viewPos.x = Mathf.Clamp(viewPos.x, screenBounds.x + playerWidth, screenBounds.x * -1 - playerWidth);
viewPos.z = Mathf.Clamp(viewPos.z, screenBounds.y + playerDepth, screenBounds.y * -1 - playerDepth);
remodel.place = viewPos;
// the above will work with the middle of the tank.
}
The tank is presently jittering between screenpoints, undecided if this has to do with the boxcollider itself.
[ad_2]