• 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 sonic220 · Apr 03, 2011 at 10:20 AM · transformmovetowards

moving a platform

Hi guys, I'm still a novice and I need help with this code, there's something I don't understand In this code I'm trying to make a platform move from left to the right in a loop, the problem I have is that when the platform must come back to is original position, instead of moving it jumps to the original place. I don't understand why

function Update () { if(!ontrigger){ if(orizz_vert){ //muovi orizzt transform.position = Vector3.MoveTowards(partenza, arrivo, Time.time * velocita); //arrivato = true; if(Vector3.Distance(transform.position, arrivo) <= 0 && arrivato){ WaitForSeconds(pausa); Debug.Log("entrato"); var niente : Vector3 = partenza; //scambio partenza e arrivo partenza = arrivo; arrivo = niente; arrivato = false; } } else { //muovi vert

     }

 }

}

Comment
Add comment
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

2 Replies

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

Answer by Statement · Apr 03, 2011 at 12:01 PM

One reason your code makes your object jump is because your MoveTowards work on the two variables that are swapped (partenza become arrivo and arrivo become partenza). First of all, this isn't how MoveTowards is meant to be used. It looks like you are sort of lerping. Second, you're using Time.time instead of Time.deltaTime. Time.deltaTime is usually used with MoveTowards, and usually you put the same variable in the first parameter as the return value. (The value being moved toward arrivo).

Your current solution would place transform.position at the new arrivo once it hits the old arrivo.
Why? Because you're using Time.time * velocita, which never reset.

transform.position = Vector3.MoveTowards(transform.position, arrivo, 
                                         Time.deltaTime * velocita);

Instead of.

transform.position = Vector3.MoveTowards(partenza, arrivo, Time.time * velocita);
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
avatar image
2

Answer by Jesse Anders · Apr 03, 2011 at 11:15 AM

I'm not sure if this is the cause of the problem, but this conditional:

Vector3.Distance(transform.position, arrivo) <= 0

Will only evaluate to 'true' if transform.position and arrivo happen to be exactly the same, which will be unlikely in the general case due to floating-point error.

Also, distance is by definition positive, so the '<' part of the comparison shouldn't be necessary.

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 sonic220 · Apr 03, 2011 at 11:29 AM 0
Share

that condition works fine, that Debug.Log show me "entrato" in the scene. I think the problem is in that transform.movetowards, but I don't know what to do

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

No one has followed this question yet.

Related Questions

Setting target position in Vector3.MoveTowards 2 Answers

Camera moveTowards stuttering in place? 1 Answer

Set speed by distance from object 1 Answer

Reposition to minimum distance from target 2 Answers

reset an object's position doesn't work fine 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