Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by zazavatar · Jun 03, 2018 at 06:54 AM · uicanvasspeedmoving

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.alt text

 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;
         }
     }
 }
 


ezgifcom-optimize.gif (515.3 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Jun 04, 2018 at 08:51 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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:

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zazavatar · Jun 04, 2018 at 10:18 AM 0
Share

Thanks, this should work fine. I didn't think to check 3D space...

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zazavatar · Jun 03, 2018 at 09:09 AM 0
Share

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.

storage/temp/118160-test.txt

test.txt (40.9 kB)
avatar image Serellyn zazavatar · Jun 03, 2018 at 09:21 AM 1
Share

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/

avatar image zazavatar Serellyn · Jun 04, 2018 at 08:22 AM 0
Share

Here is the GitHub link https://github.com/Brown121407/CarFactory$$anonymous$$anager-1.1 Thanks for trying to help.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI text vs. moving camera 1 Answer

Is there a way to scale particle systems with the cnavas size? 1 Answer

I need help with infinite scrolling backgound. 0 Answers

CanvasScaler force to rescale at runtime 0 Answers

How to deal with many buttons in the Inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges