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 thornekey · Feb 14, 2015 at 10:19 AM · physicsmoveplatform

Whats wrong with this script?

this is a moving platform script. it wont work..

     public Vector3 pointA;
     public Vector3 pointB;
 
     public float speed;
 
     void Start () {
         gameObject.transform.position = pointA;
     }
 
     // Update is called once per frame
     void Update () {
         if (gameObject.transform.position == pointA) {
             gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, pointB, speed * Time.deltaTime);
         } 
         else if (gameObject.transform.position == pointB) {
             gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, pointA, speed * Time.deltaTime);
         }
     }

I get no errors. the platform just wont move..

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 meat5000 ♦ · Feb 14, 2015 at 11:06 AM 0
Share

Value of speed is? Undefined so probably 0. Value of pointA and pointB is? Undefined.

Is this script on the platform object?

What are the chances of the platform being exactly on the point defined by your vectors? Very little I shall tell thee.

avatar image thornekey · Feb 14, 2015 at 11:29 AM 0
Share

i was setting the value in the inspector.. :) The script was on the object. The question has been solved :)

1 Reply

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

Answer by troien · Feb 14, 2015 at 11:02 AM

That is because you tell your platform to move towards pointB. So the first time you execute the Update loop, your position is equal to PointA and your platform will move a tiny little bit towards pointB, the next time you execute the Update loop, your position is not equal to PointA or pointB. Instead it is some Point in between, and thus your Update loop won't do anything after that...

I would suggest to change your code to something like this...

 public Vector3 pointA;
 public Vector3 pointB;
 
 public float speed;
 
 private Vector3 currentTarget;
 
 void Start ()
 {
     transform.position = pointA;
     currentTarget = pointB;
 }
 
 // Update is called once per frame
 void Update ()
 {
     transform.position = Vector3.MoveTowards(transform.position, currentTarget, speed * Time.deltaTime);
 
     // If we reach pointA or pointB, change target
     if (transform.position == currentTarget)
     {
         if (currentTarget == pointA)
             currentTarget = pointB;
         else
             currentTarget = pointA;
     }
 }

If it really doesn't even move the first Update loop, then either pointB is equal to pointA, speed is 0, or you have your script disabled... Either way, the problem I described above will still happen.

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 thornekey · Feb 14, 2015 at 11:20 AM 0
Share

that kind of works. but say if my point B x value is 56 it will only go to 54.99991 or something so it wont go all the way thus returning to point a.. nvm got it thanks for your 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

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

Moving Physics Objects With Character Controller? 2 Answers

Rotate multiple objects around or rather move them on the surface of another object 1 Answer

Player Controls 0 Answers

How do I stop a player from bouncing on a descending platform? 1 Answer

How to 'do stuff' to specific cross sections of 3D polygons 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