Is there any reason not to do GetAxis this way?

In almost every example I see, people are using if statements with Input.GetAxis. In the code below, the commented section is typical of what I have seen. I commented out that section and added the single line above it, and it seems to react identical. When I say typical, I’m talking about the general structure. Everyone uses different variables and values to match what suits their situation.

Its not really relevant to the question, but for those that are curious “Rotation” is an axis I setup, and its being multiplied by the rotate so that it does not instantly rotate at max speed, it works up to max rotating speed then slows down to no rotation when the button is released.
This is in Update ():

	transform.Rotate(0,0,20*Time.deltaTime*Input.GetAxis("Rotate"));

	//if(Input.GetAxis("Rotate")>0){
	//	transform.Rotate(0,0,20*Time.deltaTime*Input.GetAxis("Rotate"));
	//}else if(Input.GetAxis("Rotate")<0){
	//	transform.Rotate(0,0,20*Time.deltaTime*Input.GetAxis("Rotate"));
	//}

Would there be any disadvantages to doing the same thing with AddForce? Removing the if statements and dropping it down to a single line like above.

rigidbody.AddForce(rgt*Time.deltaTime*Input.GetAxis("Horizontal")/250);

//	if(Input.GetAxis("Horizontal")>0){
//		rigidbody.AddForce(rgt*Time.deltaTime/250);
//	}else if(Input.GetAxis("Horizontal")<0){
//		rigidbody.AddForce(-rgt*Time.deltaTime/250);
//	}

I suppose it might be slightly more efficient with if statements, if you expect that there will usually be no input (so it’s not calling the Rotate function every frame). On the other hand, it would be slightly more efficient without if statements, if you expect that there will usually be input (so it doesn’t unnecessarily execute if statements every frame). It wouldn’t make any real difference though. In any case that’s not a very clean way of using if statements; this would be better (plus it would be slightly more efficient to call GetAxis once):

var rotateAxis = Input.GetAxis("Rotate");
if (rotateAxis != 0.0) {
    transform.Rotate(0, 0, 20*Time.deltaTime*rotateAxis);
}

I would be inclined to do whatever uses the least code.

–Eric

Great explanation, thanks. So, your saying the difference simply comes down to preference, and that you would probably use the 1 liner?

I’m not sure if the rotate function uses the physics engine, but I do know AddForce does, would that make a difference? If not, I’ll be able to lay out my entire player control script in about 10 lines.

I see what you mean about not being very clean. It didnt start that way, adding the getaxis to the rotate came later, before that, the else if was needed. Is there any reason you didnt put the getaxis right into line 3?

Sorry if it seems like I’m nit-picking, that’s not the case at all. I’m a minimalist coder, the fewest lines I can do it in with the lowest resource cost is how I try to build programs from the ground up. Figured I’d keep that approach when using Unity.

Yes.

Not enough to worry about, particularly since this is (I assume) about moving just one object.

I was avoiding calling GetAxis twice.

–Eric

You have been very helpful. Thanks!