• 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
Question by MerlinsMaster · Mar 09, 2018 at 08:00 PM · uirecttransformsmoothdamp

Can't move a rectransform to an exact position

Hey Guys,

I have a scene with a set of uniformly shaped UI panels, and I am having them move up or down at a specific increment when I press the W or S keys.

The issue I am having is that they never reach their exact target position. They are always some very small decimal value off.

Is there any way for me to force it to reach the exact position?

This is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class cs_UIControl : MonoBehaviour {
 
     public RectTransform currentposition;
 
     public float smoothTime;
     public float maxSpeed;
     private Vector2 velocity;
 
     public int menuOptionID;    // the number of the current Menu Option, 0 is default
 
     public int maxMenuOptions;  // the maximum number of menu options on this screen
     public float moveIncrement;  // the distance the menu pane will move with each increment
     public RectTransform targetPosition;    // the destination point
 
     void Start () 
     {
         menuOptionID = 0;
         velocity = Vector2.zero;
     }
     
     void Update () 
     {
         PlayerControls();   
         UpdatePosition();
     }
 
     public void PlayerControls()
     {
         if(Input.GetKeyUp(KeyCode.W))
         {
             MovePositionDown();
         }
 
         if (Input.GetKeyUp(KeyCode.S))
         {
             MovePositionUp();
         }
 
         if (Input.GetKeyUp(KeyCode.A))
         {
             PreviousMenu();
         }
 
         if (Input.GetKeyUp(KeyCode.D))
         {
             SelectCurrentOption();
         }
     }
 
     public void UpdatePosition()
     {
         currentposition.anchoredPosition = Vector2.SmoothDamp(currentposition.anchoredPosition, targetPosition.anchoredPosition, ref velocity, smoothTime, maxSpeed, Time.deltaTime);
     }
 
     public void ChangeTargetPosition()
     {
         targetPosition.anchoredPosition = new Vector2(currentposition.anchoredPosition.x, currentposition.anchoredPosition.y + moveIncrement);
     }
 
     public void MovePositionDown()
     {
         moveIncrement = -160;
         menuOptionID -= 1;
         if (menuOptionID < 0)
         {
             menuOptionID = 0;
             moveIncrement = 0;
         }
             
         ChangeTargetPosition();
     }
 
     public void MovePositionUp()
     {
         moveIncrement = 160;
         menuOptionID += 1;
         if (menuOptionID > maxMenuOptions)
         {
             menuOptionID = maxMenuOptions;
             moveIncrement = 0;
         }
 
         ChangeTargetPosition();
     }
 
     public void PreviousMenu()
     {
         Debug.Log("PreviousMenu");
     }
 
     public void SelectCurrentOption()
     {
         Debug.Log("SelectCurrentOption");
     }
 }

Comment

People who like this

0 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 myzzie · Mar 09, 2018 at 08:54 PM 0
Share

Mathf.Approximately

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

129 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

Related Questions

UI Visual Not Moving With Transform 1 Answer

Getting the width of UI text inside a layout allways 0. 2 Answers

Displaced UI RectTransform collider in some resolutions issue. 1 Answer

Why Is Rotation Different For RectTransform? 0 Answers

Unable to move UI elements with cursor 0 Answers


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