- Home /
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;
}
}
Answer by syclamoth · Sep 20, 2011 at 06:45 AM
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.)
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
c# how to change player location on 0 hp 1 Answer
Set controller to active script - how do I transform him to the correct position? 1 Answer
a child object that inherits only position from parent 2 Answers
How to make a sine wave with a transform 2 Answers