- Home /
Camera moveTowards stuttering in place?
Hello! I'm trying to make my camera move from one point to another when pressing a button. When the button is pressed, it sets the variable "moveToEditPos" to true. This is what i tried so far:
void Update() {
print (moveToEditPos.ToString ());
if(moveToEditPos==true)
//if(Vector3.Distance(Camera.main.transform.position,editModePos) > 0.1f)
Camera.main.transform.position = Vector3.MoveTowards (transform.position,editModePos, 15F * Time.deltaTime);
}
I get this, however:(sorry for the quality..)
https://www.youtube.com/watch?v=MDwjbZdlaeE
It just stutters in place. Why is this happening?
Answer by TexelBox · Jul 27, 2017 at 01:10 AM
What is happening is that every frame where moveToEditPos = true
, your MoveTowards()
method is returning the same vector and moving the camera to the same position over and over. You are not "stepping" the camera towards its destination point, but rather are immediately setting its position to the same point along the line connecting transform.position
(of your script's parent gameobject) and editModePos
. Consult the documentation link below to see the proper way to use MoveTowards().
https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
You need to be incrementing the camera's position towards its destination position each frame.
Try replacing line 5 with Camera.main.transform.position = Vector3.MoveTowards (Camera.main.transform.position,editModePos, 15F * Time.deltaTime);
and see if this is the behavior that you desire.
Your answer
Follow this Question
Related Questions
Set speed by distance from object 1 Answer
Translate issue with Vector3.MoveTowards 1 Answer
Reposition to minimum distance from target 2 Answers
Move Transform to Target in X seconds 3 Answers
How to get a proper ramming effect? 1 Answer