Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by monserboy · Feb 24, 2014 at 11:50 AM · transformupdatebackgroundslowresponse

Transform position check in update is slow

Hi

I'm making an infinite background that goes down along the Y-axis. Basically the background sprite transforms back to its initial position when it has reached the maxYposition that I've setup.

However my sprite background objects moves always way past the maxYPosition I've set up. Example: maxYPos = -5.6 but it snaps back to initial position when something like -6.6 is reached.

If I test my code in a new scene it works flawless.

What could be the reason my Update() responds so slow?

My C# code:

 using UnityEngine;
 using System.Collections;
 
 public class MoveBuilding : MonoBehaviour {
 
     public float backgroundMoveSpeed = 10f;
     public double maxYPos;
     private Vector3 startPos;
 
     // Use this for initialization
     void Start () {
         startPos = transform.position;
     }
     
     // Update is called once per frame
     void Update () {
         //Moves object down
         if(transform.position.y <= maxYPos){
             transform.position = startPos;
         }
         transform.Translate(0, Time.deltaTime * -backgroundMoveSpeed, 0);
     }
 }
 
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 Invertex · Feb 24, 2014 at 12:12 PM 0
Share

Check the prefab if it's a prefab, check the inspector settings to see if maxYPos was set to a value 1 lower. The fact that it's going a distance of exactly an extra 1 unit, is a bit telling of a coding or inspector change.

Also, perhaps try sticking it in LateUpdate().

2 Replies

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

Answer by Robacks · Feb 24, 2014 at 12:10 PM

Chache your transform component. Read this guide for detailed informations. I am used to make my own derived class of MonoBehaviour which overrides these shortcut getters, and caches those references when i do the first lookup with them.

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 Supersaiyanblue · May 08, 2019 at 01:13 PM 0
Share

$$anonymous$$ATE THAN$$anonymous$$ YOU. YOU ARE A GODSEND.

avatar image
0

Answer by monserboy · Feb 24, 2014 at 01:09 PM

The answer of Robert Acksel solved it, along with teaching me a better way to program the transforms. Thank you very very much!!

Solution in C#, for future reference:

 using UnityEngine;
 using System.Collections;
 
 public class MoveBuilding : MonoBehaviour {
 
     public float backgroundMoveSpeed = 10f;
     public double maxYPos;
     private Vector3 startPos;
     private Transform transformCache;
 
     // Use this for initialization
     void Start () {
         startPos = transform.position;
     }
 
     void Awake () {
         transformCache = transform;
     }
     
     // Update is called once per frame
     void Update () {
         //Moves object down
         if(transformCache.position.y <= maxYPos){
             transformCache.position = startPos;
         }
         transformCache.Translate(0, Time.deltaTime * -backgroundMoveSpeed, 0);
     }
 }
 
Comment
Add comment · 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

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

22 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

Related Questions

Rotate Method via Update ? 2 Answers

WebGL Run in Background only works 1 update per second 1 Answer

Make an Object Animate Up In The Update Function 2 Answers

Getting updated variables from another script 2 Answers

How do i Update a Transforms Position, and how do i have objects face along a spline?? 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