- Home /
Canvas moves faster the second time it goes somewhere in the scene
I have 2 canvases in a scene. The second one is meant to come over the first, then leave. The first time it comes it moves slower than when it leaves or comes again any other time. The speed should not change anywhere in the code. I am curious about this behaviour, so if anyone could enlighten me please do it. Here is the code and a gif of the situation.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MainMenuNavigator : MonoBehaviour
{
[Header("Canvases")]
[SerializeField] private Canvas _menuCanvas;
[SerializeField] private Canvas _optionsCanvas;
[SerializeField] private Canvas _profilesCanvas;
[Header("Canvas movement options")]
[SerializeField] private float _moveSpeed = 100f;
[SerializeField] private Transform _destination;
[SerializeField] private Transform _hidingPlace;
[Header("Menu components")]
[SerializeField] private Button[] _menuButtons;
[SerializeField] private Button[] _optionsButtons;
[SerializeField] private Button[] _profilesButtons;
private Canvas _activeCanvas;
private void Start()
{
_activeCanvas = _menuCanvas;
ToggleButtons(_optionsButtons);
ToggleButtons(_profilesButtons);
}
public void SwitchToMenu()
{
StartCoroutine(MoveCanvas(_activeCanvas, _hidingPlace.position));
ToggleButtons(_activeCanvas == _optionsCanvas ? _optionsButtons : _profilesButtons);
_activeCanvas = _menuCanvas;
ToggleButtons(_menuButtons);
}
public void SwitchToOptions()
{
ToggleButtons(_menuButtons);
StartCoroutine(MoveCanvas(_optionsCanvas, _destination.position));
ToggleButtons(_optionsButtons);
_activeCanvas = _optionsCanvas;
}
public void SwitchToProfiles()
{
ToggleButtons(_menuButtons);
StartCoroutine(MoveCanvas(_profilesCanvas, _destination.position));
ToggleButtons(_profilesButtons);
_activeCanvas = _optionsCanvas;
}
private IEnumerator MoveCanvas(Canvas canvas, Vector3 newPosition)
{
while (!canvas.transform.position.Equals(newPosition))
{
canvas.transform.position =
Vector3.MoveTowards(canvas.transform.position, newPosition, _moveSpeed * Time.deltaTime);
yield return null;
}
}
private void ToggleButtons(IEnumerable<Button> buttons)
{
foreach (var button in buttons)
{
button.enabled = !button.enabled;
}
}
}
It's just a hunch, but I think canvas.transform.position.Equals(newPosition) will never evaluate to true due to floating point imprecision, so you actually never exit the while loop. Try adding a debug printout to the end of $$anonymous$$oveCanvas() to see if they do end or not.
If this is the problem, then check that the distance is below a certain threshold (for example with Vector3.Sqr$$anonymous$$agnitude(canvas.transform.position - newPosition) < 0.01f)), and if so after the while loop set the position one more time to make sure it is at the desired position.
Answer by Bunny83 · Jun 04, 2018 at 09:08 AM
I already thought that it's probably the starting location of your canvas. I just loaded up your project and it turns out that your canvas has a start position of (18,0,90) and you move it to (0,0,0). So it need to travel a much longer way. Your hiding position is just (18,0,0) See this:
Thanks, this should work fine. I didn't think to check 3D space...
Answer by Serellyn · Jun 03, 2018 at 08:45 AM
Well this is probably either your _movespeed variable or the Time.deltatime causing your issues. Easiest way to find out what the exact problem is, is to place some Debug.Log (or Print).
In the while loop Print your _movespeed, the deltatime and print the _movespeed * time.deltatime.
If one of it causes the problem, you'll know and will be able to handle it.
Added this
Debug.Log($"Speed: {_moveSpeed}, DeltaTime: {Time.deltaTime}, Product: {_moveSpeed * Time.deltaTime}");
just after the $$anonymous$$oveTowards in the while loop and I got this output... doesn't seem to be any problem with any of these variables.
That indeed seems like it should be fine. I won't $$anonymous$$d taking a look at your project if you feel comfortable sending it to me? You could also clone your project and delete everything that has nothing to do with the issue and then send it :)
If you want this, send me a pm on the forums: https://forum.unity.com/members/serellyn.65929/
Here is the GitHub link https://github.com/Brown121407/CarFactory$$anonymous$$anager-1.1 Thanks for trying to help.
Your answer