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 /
This question was closed Feb 13, 2020 at 04:48 PM by tormentoarmagedoom for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by norbertbn97 · Feb 13, 2020 at 02:32 AM · movementobject

Moving object between points

Hello, I want to make an object move from point A: Vector3(-7, 1, 153) to point B: Vector3(7, 1, 153) but i want this object to start moving from position Vector3(1, 1, 153) and then go left to A and right to B all the time.
Right now i have something like this:

 public Rigidbody cube;

 public float speed = 1f;
 Vector3 pointA;
 Vector3 pointB;
 Vector3 pointC;

 void Start()
 {
     pointA = new Vector3(1, 1, 153);
     pointB = new Vector3(-7, 1, 153);
     pointC = new Vector3(7, 1, 153);
 }

 void Update()
 {
     
     float time = Mathf.PingPong(Time.time * speed, 1);

     transform.position = Vector3.Lerp(pointA, pointB, time);
     
 }

}

but by this code my object move from starting position to the A point only and bouncing between them. How can I make it move like startingPosition>A point>B point>A point>B point etc.?
And side question, how can i change the speed of this object?

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

  • Sort: 
avatar image
1
Best Answer

Answer by JPhilipp · Feb 13, 2020 at 12:51 PM

Note how your target vectors A, B and C are really a list, so it can be beneficial to express them as such, too. In below, we're assigning the vectors to an array, and can then use a neat targetIndex to move towards them, as well as switch to the next goal when needed.


 using UnityEngine;
 
 public class Mover : MonoBehaviour
 {
     const float speed = 50f;
     Vector3[] targets = null;
     int targetIndex = 0;
     
     void Start()
     {
         targets = new Vector3[]
         {
             new Vector3(1f, 1f, 153f),
             new Vector3(-7f, 1f, 153f),
             new Vector3(7f, 1f, 153f)
         };
     }
     
     void Update()
     {
         FollowTarget();
     }
     
     void FollowTarget()
     {
         Vector3 target = targets[targetIndex];
         transform.position = Vector3.MoveTowards(
             transform.position, target, speed * Time.deltaTime);
         
         if (transform.position == target) { ChooseNextTarget(); }
     }
     
     void ChooseNextTarget()
     {
         switch (targetIndex)
         {
             case 0: targetIndex = 1; break;
             case 1: targetIndex = 2; break;
             case 2: targetIndex = 1; break;
         }
     }
     
 }


Speed can be increased by increasing the speed value at the top.


Now, it looks like you're using a rigidbody, too. If so, you normally want to try move it via either AddForce() applied in the proper direction, or its MovePosition() function, and for the latter, do so in a FixedUpdate() by using time.fixedDeltaTime. Otherwise, things like collision push forces may not be appropriately applied in the physics simulation.


Good luck!

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 norbertbn97 · Feb 13, 2020 at 03:50 PM 0
Share

Thank You sir! It's exactly what I wanted to.

Follow this Question

Answers Answers and Comments

259 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 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 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

Increase spawn object movement when player collects every 10 points 0 Answers

How to move an object with a starting and ending speed and time 0 Answers

How can I change the design of game_object at runtime? 0 Answers

How to move an object, depend to a second 0 Answers

How can i detect a collision with many Terrains?, since my collider is not working? 0 Answers


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