What's wrong with this simple position script?

It’s my first script. It’s meant to move and rotate a camera until it reaches a boundary then reverse, and so on.

private var moveLeft = true;
private var movePlusTime = 2 * Time.deltaTime;
private var rotatePlusTime = 20 * Time.deltaTime;

function Update () {
	
	if (moveLeft) {
		transform.position += (movePlusTime,0,0);
		transform.Rotate += (0,rotatePlusTime,0);
	}
	else {
		transform.position += (-movePlusTime,0,0);
		transform.Rotate += (0,-rotatePlusTime,0);
	}
	
	// check if crossed boundaries
	if (transform.position.x > 9) {
		moveLeft = false;
	}
	if (transform.position.x < 0) {
		moveLeft = true;
	}
}

1 Answer

1

The problem is that you are assigning dynamic values to your variables at the top of the function! You need to remove all the Time.deltaTimes from the top of your script, and instead use them only inside the transform.position function.

Also, the line

transform.Rotate += (0, rotatePlusTime, 0);

is not a value assignment, it is a function! It should be used like this-

transform.Rotate(0, rotatePlusTime * Time.deltaTime, 0);

Also, you should be using transform.Move in the same way-

transform.Move(movePlusTime * Time.deltaTime, 0, 0);

Thanks, syclamoth, that helped a lot in my understanding. I didn't realize the difference between transform properties and functions before. =) Working perfect now. (Used transform.Rotate and transform.Translate.)