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 /
avatar image
0
Question by BigAlecz · Jan 22, 2020 at 01:49 PM · vector3unity 2d

moveTowards not effected by maxDistanceDelta

My Object travels to its position as if I've changed the position instead of using moveTowards, despite setting x to anywhere from one trillion to 0.000000000000000001f. I've checked all of the other forum questions but still can't find any answers. Maybe you guys see something I don't.

  Transform posA;
  Transform posB;
  
  private void Update (){ 
   if (bool == true) {
                   pos.position = posA.position;
               } else {
                   pos.position = posB.position:
               }
               transform.position = Vector3.MoveTowards(transform.position, pos.position, x * Time.deltaTime);
  }


Comment
Add comment · Show 3
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 Casiell · Jan 22, 2020 at 02:29 PM 1
Share

Could you paste the rest of the code? As in the context to this fragment, where are you calling this?

avatar image BigAlecz Casiell · Jan 22, 2020 at 03:16 PM 0
Share

I am calling this in an update function. The rest of my code is an illegible mess, so I'll just simplify the shown fragment:

 Transform posA;
 Transform posB;
 
 private void Update (){ 
  if (bool == true) {
                  pos.position = posA.position;
              } else {
                  pos.position = posB.position:
              }
              transform.position = Vector3.$$anonymous$$oveTowards(transform.position, pos.position, x * Time.deltaTime);
 }
 



avatar image JasonBennett · Jan 23, 2020 at 05:35 PM 0
Share

Hi @buddrfingrs,

Your code looks solid to me. Have you tested this simplified code (just these 12 lines or so) in a separate scene, using a separate "test" version of the component, to verify that they work? I often do this when debugging to isolate code. Try doing this and keep simplifying your code until it works. And by "simplifying" I mean getting rid of the if() statement, setting pos.position manually in Start(), setting x manually in Start(), etc.

If the simplified code doesn't work, then both you and I are blindly missing something in the $$anonymous$$oveTowards method here. If the simplified code works, then you know there is something else affecting the transform, in which case I'd need to see more of your code to help figure out an answer.

-Jason

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by NVJOB · Jan 23, 2020 at 03:09 PM

This is a fully working script.

Explanatory video - https://www.youtube.com/watch?v=PXa7wMtRkiM

 using UnityEngine;
 
 public class Test : MonoBehaviour
 {
     public bool trigger;
     public float speed = 1;
     public Transform posA;
     public Transform posB;
 
     Vector3 pos;
 
     void Update()
     {
         if (trigger == true) pos = posA.position;
         else pos = posB.position;
         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
     }
 }
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 Bunny83 · Jan 23, 2020 at 03:29 PM 0
Share

It's generally a better style to have the pos variable declaration inside Update if it's not used anywhere else. You could do it easily with the ternary / conditional operator like that

 void Update()
 {
     Vector3 pos = (trigger) ? posA.position : posB.position;
     transform.position = Vector3.$$anonymous$$oveTowards(transform.position, pos, speed * Time.deltaTime );
 }

Though I assume that if the behaviour that the OP sees is that his object "jumps" to the target position, I assume that his "pos" Transform reference is actually the object he wants to move. Since he directly sets the position of that transform, the object jumps to the target immediately due to the line pos.position = posA.position;.

avatar image NVJOB Bunny83 · Jan 23, 2020 at 04:38 PM 1
Share

I took his code and put it into working form, without structural changes.

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

154 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

Related Questions

Get Width of 2d Game Object 1 Answer

Collider2D overlap with tiles in Tilemap and remove tiles 1 Answer

Inconsistent wallJumping, vector sends in wrong direction 0 Answers

Vector3.MoveTowards doesnt work 1 Answer

Finding the speed of MoveTowards 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