- Home /
Why does transform.rotate() do this?
This is the issue: I am using JavaScript and I am trying to make my camera go to different positions when pressing various buttons. It works fine with buttons 1 and 2, but when pressing button 3 it makes the camera begin to turn incessantly with no apparent goal, practically going crazy. Here is the code, if you could help me:
#pragma strict
var screenUp : boolean = false;
var cameraPosition : int = 0;
var storePosition : int;
var showCameraGUI : boolean = false;
var cameraWindow : Rect = Rect(300,100,400,300);
function OnGUI(){
if(GUI.Button(Rect(100,100,100,100), "Screen")){
if(screenUp == false){
cameraPosition = storePosition;
screenUp = true;
}
else if(screenUp == true){
storePosition = cameraPosition;
screenUp = false;
}
}
if(showCameraGUI){
GUI.backgroundColor = Color.clear;
cameraWindow = GUI.Window(0, cameraWindow, CameraFunction, "Camera");
}
}
function Update () {
if(screenUp){
showCameraGUI = true;
}
else{
showCameraGUI = false;
cameraPosition = 0;
}
if(cameraPosition == 0){
Camera.main.transform.position.x = -0.5886186;
Camera.main.transform.position.y = 0.2869148;
Camera.main.transform.position.z = -4.973199;
Camera.main.transform.rotation.x = 0;
Camera.main.transform.rotation.y = 0;
Camera.main.transform.rotation.z = 0;
}
else if(cameraPosition == 1){
Camera.main.transform.position.x = 3.509071;
Camera.main.transform.position.y = 0.2869148;
Camera.main.transform.position.z = -4.973199;
Camera.main.transform.Rotate(0,0,0);
}
else if(cameraPosition == 2){
Camera.main.transform.position.x = -4.109085;
Camera.main.transform.position.y = 0.2869148;
Camera.main.transform.position.z = -4.973199;
Camera.main.transform.Rotate(0,0,0);
}
else if(cameraPosition == 3){
Camera.main.transform.position.x = -3.526589;
Camera.main.transform.position.y = 3.941321;
Camera.main.transform.position.z = 0.6447246;
Camera.main.transform.Rotate(15,90,0);
}
}
function CameraFunction (windowID : int){
GUI.backgroundColor = Color.cyan;
if(GUI.Button(Rect(100,100,80,50), "Camera 1")){
cameraPosition = 1;
}
if(GUI.Button(Rect(100,170,80,50), "Camera 2")){
cameraPosition = 2;
}
if(GUI.Button(Rect(200,100,80,50), "Camera 3")){
cameraPosition = 3;
}
}
You know that "spastically" isn't an accepted word? I didn't reject your question, believing that you'd quickly edit your question.
So, you would have rejected my WHOLE question due to one word?
Is there any way I could achieve my desired result, which is having it rotate to a certain degree instantly, by using transform.rotation?
Answer by Graham-Dunnett · Jan 09, 2015 at 11:34 AM
Presumably line 57 gets called every frame.
The above answer is the actual cause, though I'll explain what is happening. The script is in the update function, and so is getting called each frame. The function "Rotate" simply rotates from whatever angle it currently is at. Notice in the other if statements you only put 0 as the arguments, so it didn't make any change at all. It works relative to whatever the angle is at the moment, not an actual "Set" of the rotation. But then in the first if statement you directly set the x/y/z of the rotation, which I believe is what you want. So, you should do the same in the other if statement.
The other thing is that you only want to set the position and rotation once, not directly in the update function usually. Once you set the position, unless you or the physics engine does something, it won't change on you. So when you are checking the input, and setting the variable for the position, just do the actual position/rotation change there. That way, the code only gets ran once, not in every frame in the update function.
I did use transform.rotation for all of them at the start, hence the first one doing that, but it gave me an undesired result, setting the camera's rotation to a completely different angle from what I had asked. I did assume the Update function to be the issue with transform.rotate, but I didn't know how to fix it. If you could enlighten me on how I could get transform.rotation to work properly, I would greatly appreciate it.
Your answer
Follow this Question
Related Questions
transform.Rotate For an Certain Amount of Time 1 Answer
How to spawn objects on a restricted sphere surface relative to camera? 1 Answer
getting the position of player for the minimap to follow 1 Answer
Setting a cube to be exactly size of intersecting camera view plane 1 Answer
GUI.Button child of the camera . 0 Answers