- Home /
Player bounds optimization and code review
Hello unity community! I need your help in review my code and give me some advices if i can improve it somehow.
I'm following unity course and the guy did this script to check bounds:
if(transform.position.x < -boundRange)
{
transform.position = new Vector3(-boundRange, transform.position.y, transform.position.z);
}
if(transform.position.x > boundRange)
{
transform.position = new Vector3(boundRange, transform.position.y, transform.position.z);
}
A found a bit different way to achieve the same thing with only one if statement like that:
if(Mathf.Abs(transform.position.x) + .1 > boundRange)
transform.position = new Vector3(
transform.position.x / Mathf.Abs(transform.position.x) * boundRange, transform.position.y, transform.position.z
);
And i want to understand which way is better and why and give me some more recommendations about good practices
Comment