Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by amonnone · Apr 28, 2017 at 06:03 AM · move an objectvectorspingpong

How to move an object back and forth along a vector

I am new to Unity and coding in general, so I apologize if this question is poorly worded.

I am trying to move an object back-and-forth repetitively between two other objects. So far my thinking is this: 1) Determine the desired path between the two objects by calculating the equation of the line between them. (y = mx + b, b = 0) 2) use an oscillatory function like Sin(x) or Mathf.PingPong to modulate the value plugged in for x.

I would like some feedback on this idea, and if you know how to implement it that would also help! Is there an easier way to accomplish what I'm after?

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
4

Answer by Jawchewa · Apr 28, 2017 at 06:29 AM

I would maybe consider using Vector3.Lerp() to move between the two objects. So something like this:

 public class Lerper : MonoBehaviour {
     Vector3 pointA = new Vector3(0, 0, 0);
     Vector3 pointB = new Vector3(1, 1, 1);
     void Update()
     {
         transform.position = Vector3.Lerp(pointA, pointB, Mathf.PingPong(Time.time, 1));
     }
 }

where pointA and pointB would be replaced with positions of your two gameObjects. There is also a Lerp function for Vector2, if you are in 2D. You might also need to do some additional math to control the speed, as Lerping will move your object at different speeds depending on the distance between the two points.

There's actually a lot of ways that you could go about implementing this, but this is just the first one I came up with off the top of my head.

More Details on Lerp here: https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

Comment
Add comment · Show 2 · 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 amonnone · May 07, 2017 at 09:43 PM 0
Share

Thanks so much! This works perfectly. I have a different, general question which is unrelated that maybe you can help me with. For some reason when I hit "play" in my game, it seems that some of the scripts that affect game object motion are initialized, while others need to be un-checked and re-checked in the Unity editor in order to start working. Do you have any idea why this might be? Not sure if it's a common issue or not.

avatar image Jawchewa amonnone · May 07, 2017 at 10:24 PM 0
Share

Sorry., I don't think I've seen that issue before, so, I have no idea how to solve it. $$anonymous$$aybe post a new question with more details to see if anyone else has any wisdom about that problem.

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

99 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Angle between two Vectors split up into x,y,z components 0 Answers

Unity 2d make four directions (Up, Down, Right, Left. like RPG games) GUI buttons. Please help me. 1 Answer

How to spawn in precise interval for moving objects 0 Answers

Object heading towards mouse click/ touch position, script improvement. 1 Answer

How can I move a model after hitting play?,How do I move a model around after hitting play to test its physics? 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