Reputation:
is that a way to refactor the code below
var foot = foot.transform.position;
var hand = hand.transform.position;
if (distance > 0.5)
{
foot = hand;
foot.transform.Translate(x , y 0);
}
else
{
foot.transform.Translate(x , y , 0);
}
like thie below
var foot = foot.transform.position;
var hand = hand.transform.position;
distance > 0.5 ? {
foot = hand,
foot.transform.Translate(x , y 0)
}
: foot.transform.Translate(x , y 0);
or more clean code/.?
Upvotes: 0
Views: 91
Reputation: 2371
This may be an option:
var instance = distance > 0.5 ? hand.transform.position : foot.transform.position;
instance.transform.Translate(x, y, 0);
Upvotes: 1
Reputation: 74375
I might take this:
var foot = foot.transform.position;
var hand = hand.transform.position;
if (distance > 0.5)
{
foot = hand;
foot.transform.Translate(x , y 0);
}
else
{
foot.transform.Translate(x , y , 0);
}
and refactor it to something like this:
var p = ( distance > 0.5 ? hand : foot ).transform.position ;
p.transform.Translate( x, y, 0 ) ;
Because conciseness of expression has a utility all its own.
You could even make it a "one-liner", but it just reads weird:
( distance > 0.5 ? hand : foot ).transform
.position
.transform
.Translate( x, y, 0 )
;
The former reads better
Upvotes: 0
Reputation: 17579
var limb = foot.transform.position;
if (distance > 0.5)
{
limb = hand.transform.position;
}
limb.transform.Translate(x, y, 0);
No need for ternary operators either.
Upvotes: 0