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 NewDevDave · Jul 24, 2017 at 01:18 PM · vector3.movetowards

I am having an issue with moving a game object using MoveTowards.

I am in the latter stages of development for my 2D platform game and am currently working on a boss battle. I have previously used Vector3.MoveTowards for various things such as enemies and moving platforms and have had no issues but for some reason the movement for this object is just not working and I can't figure out why not. Every other part of the script works absolutely perfectly but the movement is just not happening. Could someone please help me to figure out what I must be missing? Scripts are added below.

First, the movement script

 {
     public Vector3 posA;
     public Vector3 posB;
     public Vector3 posC;
     public float speed;
     [SerializeField]
     private Transform childTransform;
     public bool movingright = false;
     public bool movingleft = false;
     public Vector3 currentpos;
     public Vector3 nextpos;
     private SpriteRenderer sidsprite;
     public Animator sidanim;
     public Transform[] waypoints = new Transform[3];
 
     // Use this for initialization
     void Start ()
     {
         currentpos = waypoints[0].localPosition;
         nextpos = waypoints[1].localPosition;
         posA = waypoints[0].localPosition;
         posB = waypoints[1].localPosition;
         posC = waypoints[2].localPosition;
         sidsprite = GetComponentInChildren<SpriteRenderer>();
         sidanim = GetComponentInChildren<Animator>();
     }
 
     public void FindNext()
     {
         if (currentpos == posA)
         {
             nextpos = posB;
             Debug.Log("Next Position is " + nextpos);
         }
         if (currentpos == posB)
         {
             nextpos = posC;
             Debug.Log("Next Position is " + nextpos);
         }
         if (currentpos == posC)
         {
             nextpos = posA;
             Debug.Log("Next Position is " + nextpos);
         }
     }
 
     public void Flip()
     {
         if (nextpos == posA | nextpos == posB)
         {
             sidsprite.flipX = true;
             movingright = true;
         }
         else if (nextpos == posC)
         {
             sidsprite.flipX = false;
             movingleft = true;
         }
     }
 
     public IEnumerator Movements(Transform[] waypoints)
     {
         sidanim.SetBool("IsIdle", false);
         sidanim.SetBool("IsMoving", true);
         if (Vector3.Distance(currentpos, nextpos) >= 0.01f)
             childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nextpos, speed * Time.deltaTime);
         if (Vector3.Distance(childTransform.localPosition, nextpos) < 0.01f)
         {
             movingright = false;
             movingleft = false;
             sidanim.SetBool("IsIdle", true);
             sidanim.SetBool("IsMoving", false);
             yield return new WaitForSeconds(1);
             FindNext();
         }
     } 
 }

All of the relevant transforms and everything is added in the inspector and everything works apart from the character does not move. The animations happen and the wait times are correct and it finds the co-ordinates for the current and next positions as it should.

The movement is called through a separate script to activate the coroutine

 public class StartBossRoutine : MonoBehaviour
 {
     private SidMovement move;
 
     // Use this for initialization
     void Start ()
     {
         move = FindObjectOfType<SidMovement>();
     }
     
     public IEnumerator StartMove()
     {
         move.sidanim.SetBool("IsIdle", true);
         move.FindNext();
         yield return new WaitForSeconds(2);
         move.Flip();
         StartCoroutine(move.Movements(move.waypoints));
         Debug.Log("Should be moving");
     }
 }
 

This also works fine and the sprite flips at the correct time and the debug prints that the character should be moving but HE DOESN'T!!

Any assistance would be greatly appreciated.

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
0
Best Answer

Answer by Yoshinator2 · Jul 24, 2017 at 07:58 PM

The answer is simply that your MoveTowards is only being called once. the MoveTowards function does not constantly move the Vector3's toward each other without being called. Instead, each time the MoveTowards is called, it moves the vector closer to the target by the scale defined in the function. To fix this, just make it run the movetowards over and over until the object meets its target.

If this solved your problem, please accept my answer. If not, feel free to ask any more questions :)

Comment
Add comment · Show 6 · 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 NewDevDave · Jul 25, 2017 at 03:14 PM 0
Share

Hi, thanks for the response, it makes perfect sense. However, I have spent the last few hours trying various different ways to do this and can't seem to get him to move at all. Usually I would just add a Boolean and set it to true to make the movement happen or just add the movement to Update but neither of these options seem to work. I am currently trying Invoke Repeating but I'm not 100% if I am using it correctly and it still isn't working! Could you please give me an idea as to how to implement your suggestion with code given as everything I keep trying seems to lead to me changing the code completely and not solving the issue?

avatar image Yoshinator2 NewDevDave · Jul 25, 2017 at 04:06 PM 0
Share

Sure. Try something along the lines of:

 public float speed;
 public GameObject nextTarget;
 private bool should$$anonymous$$ove = false;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     if (should$$anonymous$$ove)
     {
         //$$anonymous$$oves the position to the target
         this.transform.position = Vector3.$$anonymous$$oveTowards(this.transform.position, nextTarget.transform.position, speed*Time.deltaTime);
         //If the position has reached the target, this will run, stopping the function.
         if(this.transform.position == nextTarget.transform.position)
         {
             should$$anonymous$$ove = false;
         }
     }
 }
 //Call this whenever you want to start the movement
 public void start$$anonymous$$oving()
 {
     should$$anonymous$$ove = true;
 }
avatar image Yoshinator2 NewDevDave · Jul 26, 2017 at 10:13 PM 0
Share

Did that solve your problem?

avatar image NewDevDave Yoshinator2 · Jul 27, 2017 at 01:57 PM 0
Share

Sorry I have only just got a chance to work on it again, I'll let you know shortly. Thanks again

EDIT: I still cannot him to move. I have tried using the script you gave me and still nothing and I have scaled it right back and removed the animator component but the character just won't move?!

Show more comments

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

69 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

Related Questions

MoveTowards won't translate on the y axis and lerp leaves character jittering in mid-air 0 Answers

My first game. Pong paddle question. 0 Answers

how to stop player from automatically jumping on a moving platform 0 Answers

Vector3.MoveTowards, y does not reach target 1 Answer

Error with moving object to mouse clicked coordinates. [SOLVED] 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