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 Grimrender · Mar 13, 2014 at 12:12 AM · move an objectpoint-a-to-b

What is the simplest way to move an object from Point A to Point B? (2D) (JS)

This is an absurdly simple question so I apologize, but all of my searches yield more complicated answers for more difficult problems that I can't understand without understanding this.

I have a game object, as soon as the object is instantiated I want it to move from point A to point B, just once, and sit there at point B:

Point A is the point at which the object is instantiated, it's a Vector3 (or at least I think it should be; Vector2 has been giving me fits so I've just been using Vector3 and placing 0 on the z axis). But let's just say it's (1,1,0), because for the purpose of my question it doesn't matter what the actual value is.

Point B is a randomly chosen point, but let's just say it's the origin (0,0,0).

All I want to do for the time being is have the object move from point A to point B either over some period of time or at some speed. I already know that I will need to define a public variable "speed" and multiply something by that, or fill out a period of time that it should take to move from point to point. That's all I can figure out though. I'm not even quite sure if I should store this all in function Start, function Update (hopefully not), or if I'll need to use a loop to get it working. The action will be taking place completely independently of user input so I don't think it'll go in function update or require a loop, but I'm quite new so I may be mistaken.

Many thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Grimrender · Mar 13, 2014 at 06:01 AM

I managed to kind-of get my script working, in case anyone comes across this question in the future. It looks something like this:

    #pragma strict
     
         var pointB : Vector3;
         var maxSpeed: float = 1.0;
         
         function Update () {
             var change = maxSpeed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, pointB, change);
         }

Every frame the object moves from it's transform.position (point A) towards pointB at a rate controlled by maxSpeed and Time.deltaTime (as near as I can tell, anyway, that's what's happening). However this hasn't proven useful to me so-far as it can't output any sort of speed for use with an animation (again, as far as I can tell). I'm looking into doing this other ways, maybe using a ridigbody2D? I'll post back if I manage to figure out a more appropriate and useful answer.

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 robertbu · Mar 13, 2014 at 06:03 AM 0
Share

Perfectly good answer. You might also take a look at the $$anonymous$$oveObject script in the Unity Wiki. In addition, if all you are interested in is the moving and not the code, take a look at iTween. It is free in the Asset store.

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

20 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

Related Questions

Smooth Movement Transition 2 Answers

Automatic Movement Script 1 Answer

Increase speed in coroutine gives side-effect in movement 0 Answers

Moving an object a specific amount of units. 1 Answer

Move objects with mouse while paused (2D) 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