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 greatwhiteshark17283 · Oct 13, 2014 at 07:47 PM · javascriptmovementrise

Why doesn't this motion script work?

I have a simple script that makes an object go up and down repeatedly. However, the object keeps either gradually sinking or gradually rising. Eventually, the object will be nowhere near where it originally was. This is the script:

 #pragma strict
 var speed = 2.0;
 var duration = 1.0;
 private var rising = 0;
 var spawn : GameObject;
 spawn = new GameObject();
 spawn.transform.position = transform.position;
 
 InvokeRepeating("Change", duration, duration);
 
 function Update () {
     if(rising > 1){
         rising = 0;
     }
     if(rising == 0){
         transform.Translate(Vector3.up * speed * Time.deltaTime);
     }
     else{
         transform.Translate(Vector3.down * speed * Time.deltaTime);
     }
 }
 
 function Change () {
     rising ++;
     if(rising == 0){
         transform.position.y = 0;
         PrefabUtility.RevertPrefabInstance(gameObject);
     }
 }

I added in the variable of "spawn" to try and prevent this from happening, but it didn't do anything. I also tried reverting the object to its prefab every time it lowered, but that didn't do anything either. Does anybody know what's wrong with the script?

Comment
Add comment · Show 2
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 767_2 · Oct 13, 2014 at 08:00 PM 0
Share

where you call your InvokeRepeating , put it in function Start() or any other function

avatar image greatwhiteshark17283 · Oct 13, 2014 at 08:05 PM 0
Share

The problem isn't trying to get the object to move, it's trying to get it so then the object doesn't gradually start moving away.

2 Replies

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

Answer by tanoshimi · Oct 13, 2014 at 08:49 PM

You haven't currently got your call to InvokeRepeating inside a function (as @767_2 commented), so Change() is never getting called. That's why your object keeps rising or falling.

Although, TBH, if all you're trying to do is get a repeating rising/falling loop, you could achieve it much easier using Mathf.PingPong. Then your entire code could be replaced with:

 var speed = 2.0;
 var duration = 1.0;

 function Update () {
   transform.position = Vector3(transform.position.x, Mathf.PingPong(Time.time, duration) * speed, transform.position.z);
 }
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 greatwhiteshark17283 · Oct 13, 2014 at 08:59 PM 0
Share

Actually, the Invoke Repeating worked fine and the object does go up and down; just after a while the object shifts away from where it originally was. But I like the $$anonymous$$athf.PingPong, so I'll just use that ins$$anonymous$$d. Thanks for the script!

avatar image JoeStrout · Oct 13, 2014 at 09:25 PM 1
Share

I'd forgotten about $$anonymous$$athf.PingPong -- that's a great tip!

avatar image greatwhiteshark17283 · Oct 14, 2014 at 12:26 AM 0
Share

Do you know how to make it so then the y value alternates with a number besides 0?

avatar image
1

Answer by JoeStrout · Oct 13, 2014 at 08:55 PM

You're getting cumulative error. That is, the way you've set this up, if there's any previous error from the last cycle, then the next cycle just adds to it. Then the next cycle adds more error to that, and so on.

So where does this error come from? Well, you're moving at a constant speed on every frame, but the amount of that movement doesn't have any hard limit. You just switch directions every now and then. So, you'll never be exactly however-many meters you imagine from the origin when you switch -- you'll be a little more or less than that. Thus, error, added to the previous errors. Your object's average position does a random walk.

To fix this, I would recommend a completely different approach. Instead of defining a speed and duration, define the speed plus upper and lower limits of where the object should go. So, the code would look something like this:

 public float minY = 0f;
 public float maxY = 2f;
 float dir = 1f;
 
 void Update() {
     transform.Translate(new Vector3(0, dir, 0));
     if ((dir > 0 && transform.position.y > maxY)
      || (dir < 0 && transform.position.y < minY)) {
         dir = -dir;
     }
 }

No need for InvokeRepeating, and no drift!

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

30 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

Related Questions

Rotate character to the moving direction problems? 2 Answers

Movement using rigidbody.velocity to apply a constant force until stop 1 Answer

Make Lerp or other more fluid or continuous 3 Answers

Networking Synchronize Problem. 0 Answers

Rotation and movement? 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