How to make object move toward target, then toward another target?












0















I have an object that I'm trying to make move toward another object, then back to the original position. I have two gameobjects set-up as target, but with this code it moves toward the object then never returns. The targets are public Transform variables.



I would assume the problem is that the position will never be exactly even with the target position because the speed variable is 20f, but how can I trigger the change of direction without knowing exactly where the object will end up after the animation completes?



if (isMoveUpAndDown)
{
target = upTarget;
float step = speed * Time.deltaTime;
testObject.transform.position =
Vector3.MoveTowards(testObject.transform.position, target.position,
step);
if (testObject.transform.position == target.position)
target = downTarget;

}









share|improve this question




















  • 1





    instead of testObject = downTarget; you probably mean target = downTarget; (in the last line... ) and you should place the target = upTarget; (3rd line) somewhere else.

    – J. van Langen
    Nov 14 '18 at 19:08











  • Check if the two positions are closer than step (before moving) and turn around if that is the case.

    – Nico Schertler
    Nov 14 '18 at 19:10











  • That was it! Thank you!

    – NomadicBinary
    Nov 14 '18 at 19:11











  • @J.vanLangen You can convert that comment to an answer if you'd like.

    – Draco18s
    Nov 14 '18 at 22:25











  • @Draco18s yes, but I'm missing too much information to provide a complete answer.

    – J. van Langen
    Nov 15 '18 at 8:41
















0















I have an object that I'm trying to make move toward another object, then back to the original position. I have two gameobjects set-up as target, but with this code it moves toward the object then never returns. The targets are public Transform variables.



I would assume the problem is that the position will never be exactly even with the target position because the speed variable is 20f, but how can I trigger the change of direction without knowing exactly where the object will end up after the animation completes?



if (isMoveUpAndDown)
{
target = upTarget;
float step = speed * Time.deltaTime;
testObject.transform.position =
Vector3.MoveTowards(testObject.transform.position, target.position,
step);
if (testObject.transform.position == target.position)
target = downTarget;

}









share|improve this question




















  • 1





    instead of testObject = downTarget; you probably mean target = downTarget; (in the last line... ) and you should place the target = upTarget; (3rd line) somewhere else.

    – J. van Langen
    Nov 14 '18 at 19:08











  • Check if the two positions are closer than step (before moving) and turn around if that is the case.

    – Nico Schertler
    Nov 14 '18 at 19:10











  • That was it! Thank you!

    – NomadicBinary
    Nov 14 '18 at 19:11











  • @J.vanLangen You can convert that comment to an answer if you'd like.

    – Draco18s
    Nov 14 '18 at 22:25











  • @Draco18s yes, but I'm missing too much information to provide a complete answer.

    – J. van Langen
    Nov 15 '18 at 8:41














0












0








0








I have an object that I'm trying to make move toward another object, then back to the original position. I have two gameobjects set-up as target, but with this code it moves toward the object then never returns. The targets are public Transform variables.



I would assume the problem is that the position will never be exactly even with the target position because the speed variable is 20f, but how can I trigger the change of direction without knowing exactly where the object will end up after the animation completes?



if (isMoveUpAndDown)
{
target = upTarget;
float step = speed * Time.deltaTime;
testObject.transform.position =
Vector3.MoveTowards(testObject.transform.position, target.position,
step);
if (testObject.transform.position == target.position)
target = downTarget;

}









share|improve this question
















I have an object that I'm trying to make move toward another object, then back to the original position. I have two gameobjects set-up as target, but with this code it moves toward the object then never returns. The targets are public Transform variables.



I would assume the problem is that the position will never be exactly even with the target position because the speed variable is 20f, but how can I trigger the change of direction without knowing exactly where the object will end up after the animation completes?



if (isMoveUpAndDown)
{
target = upTarget;
float step = speed * Time.deltaTime;
testObject.transform.position =
Vector3.MoveTowards(testObject.transform.position, target.position,
step);
if (testObject.transform.position == target.position)
target = downTarget;

}






c# unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:09







NomadicBinary

















asked Nov 14 '18 at 19:01









NomadicBinaryNomadicBinary

275




275








  • 1





    instead of testObject = downTarget; you probably mean target = downTarget; (in the last line... ) and you should place the target = upTarget; (3rd line) somewhere else.

    – J. van Langen
    Nov 14 '18 at 19:08











  • Check if the two positions are closer than step (before moving) and turn around if that is the case.

    – Nico Schertler
    Nov 14 '18 at 19:10











  • That was it! Thank you!

    – NomadicBinary
    Nov 14 '18 at 19:11











  • @J.vanLangen You can convert that comment to an answer if you'd like.

    – Draco18s
    Nov 14 '18 at 22:25











  • @Draco18s yes, but I'm missing too much information to provide a complete answer.

    – J. van Langen
    Nov 15 '18 at 8:41














  • 1





    instead of testObject = downTarget; you probably mean target = downTarget; (in the last line... ) and you should place the target = upTarget; (3rd line) somewhere else.

    – J. van Langen
    Nov 14 '18 at 19:08











  • Check if the two positions are closer than step (before moving) and turn around if that is the case.

    – Nico Schertler
    Nov 14 '18 at 19:10











  • That was it! Thank you!

    – NomadicBinary
    Nov 14 '18 at 19:11











  • @J.vanLangen You can convert that comment to an answer if you'd like.

    – Draco18s
    Nov 14 '18 at 22:25











  • @Draco18s yes, but I'm missing too much information to provide a complete answer.

    – J. van Langen
    Nov 15 '18 at 8:41








1




1





instead of testObject = downTarget; you probably mean target = downTarget; (in the last line... ) and you should place the target = upTarget; (3rd line) somewhere else.

– J. van Langen
Nov 14 '18 at 19:08





instead of testObject = downTarget; you probably mean target = downTarget; (in the last line... ) and you should place the target = upTarget; (3rd line) somewhere else.

– J. van Langen
Nov 14 '18 at 19:08













Check if the two positions are closer than step (before moving) and turn around if that is the case.

– Nico Schertler
Nov 14 '18 at 19:10





Check if the two positions are closer than step (before moving) and turn around if that is the case.

– Nico Schertler
Nov 14 '18 at 19:10













That was it! Thank you!

– NomadicBinary
Nov 14 '18 at 19:11





That was it! Thank you!

– NomadicBinary
Nov 14 '18 at 19:11













@J.vanLangen You can convert that comment to an answer if you'd like.

– Draco18s
Nov 14 '18 at 22:25





@J.vanLangen You can convert that comment to an answer if you'd like.

– Draco18s
Nov 14 '18 at 22:25













@Draco18s yes, but I'm missing too much information to provide a complete answer.

– J. van Langen
Nov 15 '18 at 8:41





@Draco18s yes, but I'm missing too much information to provide a complete answer.

– J. van Langen
Nov 15 '18 at 8:41












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307087%2fhow-to-make-object-move-toward-target-then-toward-another-target%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307087%2fhow-to-make-object-move-toward-target-then-toward-another-target%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma