- Home /
Camera not moving smoothly from point a to point b
In my scene I have a small room. Depending on the menu items you choose I want the menu to close, the camera to move to its next destination, and the menu to open again with its new options.
Currently the camera does move to the correct destination but it's an instantaneous switch. The camera just kind of teleports over there instead of moving slowly.
Here is the code I have.
This is an example of a menu window function.
void playWindow(int windowID)
{
//Leave a gap from the header.
GUILayout.Space(15);
if(GUILayout.Button("Practice", GUILayout.Height(buttonHeight)))
{
//Play against AI
}
GUILayout.Space(10);
if(GUILayout.Button("Online", GUILayout.Height(buttonHeight)))
{
//find a partner
}
GUILayout.Space(10);
if(Application.isWebPlayer == false)
{
if(GUILayout.Button("Back", GUILayout.Height(buttonHeight)))
{
target = startCam ;
showPlayMenu = false ;
}
}
}
Here you'll notice that the target variable is set to 'startCam' when the 'back' button is pushed.
This is part of the scripts update() function that causes the camera to move.
void Update () {
int userId = loginScript.userId ;
bool loggedIn = loginScript.loggedIn ;
if (loggedIn == false){
Application.Quit();
}
//Debug.Log ("ran");
if (loggedIn == true) {
if (showBarMenu == false && showDeckMenu == false && showPlayMenu == false && playing == false && buildingDeck == false ){
showMainMenu = true ;
}
}
float step = cameraSpeed * Time.deltaTime;
Camera.main.transform.position = Vector3.MoveTowards (target.transform.position, target.position, step);
//TODO: camera does not move smoothly.
//rotate camera to be the same rotation as targetCam
mainCam.transform.rotation = target.transform.rotation ;
}
The camera's rotation happens instantly and I just through that code in there because the previous movement code wasn't functioning correctly.
I realize that that will not work.
Is the vector3 movement not working because I need to tell it to continue on the next frame some how? I thought Time.deltatime would do that for me. How do I make the camera glide to its new destination instead of appear there?
float step = cameraSpeed * Time.deltaTime;
Your step is a constant. You will only get one movement from your step.
Update() is automatically called once a frame. You need to change step each frame.
Answer by flaviusxvii · Jul 06, 2014 at 02:06 PM
Camera.main.transform.position = Vector3.MoveTowards(target.transform.position, target.position, step);
First of all, these variable names are super-silly. This reads as.. moving from target which is a GameObject maybe.. to target.. which is a transform. So there are two things named target and they're not the same type of thing. That can't possibly work. It's bad. Super bad. Know this is really bad.
You should try something more like this:
Camera.main.transform.position = Vector3.MoveTowards(Camera.main.transform.position, target.position, step);
That just about completely fixed the problem. I was moving it from its target to its target which was getting me that teleport like behavior.
Thanks flaviusxvii.
Now i'll fumble my way through the rotation as well.
Your answer
Follow this Question
Related Questions
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers
How do I add a camera bounce effect every time the player lands? 0 Answers
Rotate 3rd person camera around player 1 Answer
Constrain a GameObject's movement to an orthographic camera's viewport bounds 4 Answers
RTS Mouse Click Movement 1 Answer