- Home /
Smooth Movement Transition
I am trying to move the an object to the "next" point (0,6) once a certain amount of rotations has occurred. I can get the object to move to the point, but it happens instantly and not smooth like the rotation. I tried using MoveTowards, but haven't been able to make it work. What could I add?
Here's what I have currently:
if (rotationCounter < 6) {
timeCounter += Time.deltaTime * speed;
float x = Mathf.Cos (timeCounter) * width;
float y = Mathf.Sin (timeCounter) * height;
float z = 0;
transform.position = new Vector3 (x, y, z);
if (buttonDown == true) {
transform.position = new Vector3 (next.transform.position.x, next.transform.position.y, z);
}
Answer by Namey5 · Nov 03, 2016 at 06:09 AM
For smooth transitions you would ordinarily use interpolation, or lerping.
bool move = false;
float speed = 1;
void Update ()
{
...
if (buttonDown)
move = true;
if (move)
transform.position = Vector3.Lerp (transform.position, Vector3 (next.transform.position.x, next.transform.position.y, z), Time.deltaTime * speed);
if (Vector3.Distance (transform.position, next.transform.position) < 0.02 && move)
move = false;
...
}
Thanks for getting back to me. I checked out the unity manual and was having difficulty understanding why you put in this value for the target value - Vector3 (next.transform.position.x, next.transform.position.y, z) in line 11/12. I was unable to build the code due to have a type (Vector3) where a value should be. I changed that part to next.transform.position, but the ship doesn't teleport nor move to the target point. I may be using the code you provided in the wrong area.
Here's what I have:
void Update () {
Rocket$$anonymous$$ove ();
if (buttonDown == true) {
move = true;
StartCoroutine (CountDown ());
}
if (move == true) {
transform.position = Vector3.Lerp (transform.position, next.transform.position, Time.deltaTime * speed);
}
if (Vector3.Distance (transform.position, next.transform.position) < 0.02 && move) {
move = false;
}
}
public void Rocket$$anonymous$$ove(){
timeCounter += Time.deltaTime * speed;
float x = $$anonymous$$athf.Cos (timeCounter) * width;
float y = $$anonymous$$athf.Sin (timeCounter) * height;
float z = 0;
transform.position = new Vector3 (x, y, z);
}
public void NextPlanet(){
level++;
buttonDown = true;
startCountDown = false;
}
I used that value ("Vector3 (next.transform.position.x, next.transform.position.y, z);") because it is the position that you supplied in the original question code snippet. Also, I forgot this was C# and you have to add the "new" keyword before the Vector3 reference. As for the code in your comment, it is completely correct, so there's no real reason why it wouldn't work unless
The "speed" variable is set to 0.
Your object is within 0.02 distance to the "move" transform initially.
There is a problem with the "move" boolean.
The reason I have a distance check for the second is that interpolation (in this case) becomes slower the closer you get to the target, and as such you can never actually get to the exact position without making it fixed. As for the "move" boolean, you should just check that in the inspector it is being enabled.
I tried debugging and the if(move == true) executes, but the ship shakes a little and then continues to orbit. The if(Vector3.Distance) never executes.
Here's the code:
void Start () {
countDownTimer.enabled = false;
planetHop.enabled = false;
planetHop.image.enabled = false;
speed = 1f;
height = 3f;
width = 3f;
}
// Update is called once per frame
void Update () {
Rocket$$anonymous$$ove ();
Face$$anonymous$$oveDirection();
if (orbitCounter == 6) {
Destroy (gameObject);
}
if (buttonDown == true && level==0) {
move = true;
StartCoroutine (PlanetHopCountDown ());
level++;
Debug.Log (move);
}
if (move == true) {
transform.position = Vector3.Lerp (transform.position, new Vector3 (next.transform.position.x, next.transform.position.y, 0), Time.deltaTime * speed);
Debug.Log ("Traveling");
}
if (Vector3.Distance (transform.position, next.transform.position) < 0.02f && move == true) {
Debug.Log (move);
move = false;
}
if (startCountDown == false && level == 1) {
countDownTimer.enabled = false;
StopCoroutine (PlanetHopCountDown ());
}
}
public void Rocket$$anonymous$$ove(){
if (rotationCounter < 6) {
timeCounter += Time.deltaTime * speed;
float x = $$anonymous$$athf.Cos (timeCounter) * width;
float y = $$anonymous$$athf.Sin (timeCounter) * height;
float z = 0;
transform.position = new Vector3 (x, y, z);
}