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
0
Question by jenniasanders · Nov 11, 2015 at 01:33 PM · gameobjectsif statementmovetowards

Cant stop Movetowards, already tried transform.position=end.position

I'd like the Movetowards code to stop when it arrives at the endPoint.

I added an if(transform.position == endPoint.position) however this just makes the movetowards stop after 1 frame - without ever reaching the endPoint.

Here's my code (Move only occurs on a double click, so has a click counter first) What am I doing wrong?! Thanks!

 using UnityEngine;
 using System.Collections;
 
 public class MoveReset : MonoBehaviour {
     
     int mouseClicks = 0;
     float mouseTimer = 0f;
     float mouseTimerLimit = .25f; 
     
     bool shouldMove;
     bool shouldReturn;
     
     public Transform startPoint;
     public Transform endPoint;
     public float speed;
     
     Vector3 pos0;
 
     void Update() {
 
         float step = speed * Time.deltaTime; //Lerp Speed
 
 
             if(Input.GetMouseButtonDown(0))
         {
             mouseClicks++;
         }
         
         if(mouseClicks >= 1 && mouseClicks < 3) //detects if a double click
         {
             mouseTimer += Time.fixedDeltaTime;
             
             if(mouseClicks == 2)
             {
                 if(mouseTimer - mouseTimerLimit  < 0)
                 {
                     Debug.Log("Mouse Double Click");
                     mouseTimer = 0;
                     mouseClicks = 0;
 
                     checkpos(); //attempt to store orignal starting position
                     shouldMove = true;
                     shouldReturn = false;
                 }
             }
             
             if(mouseTimer > mouseTimerLimit)
             {
                 Debug.Log("Single Click"); //single click does nothing yet
                 mouseClicks = 0;
                 mouseTimer  = 0;
             }
             
         }
 
         if (Input.GetMouseButtonDown (1)) { //returns to original position on right-click
             Debug.Log ("Right Click");
             
             shouldReturn = true;
             shouldMove = false;
         }
 
         if(shouldMove) //move to new position
         {
             Debug.Log ("Moving");
             transform.position = Vector3.MoveTowards(startPoint.position, endPoint.position, step);
         }
 
 
         if (shouldReturn) { //return to start position
             Debug.Log ("Returning");
             transform.position = Vector3.MoveTowards(startPoint.position, pos0, step);
         }
 
         if(transform.position == endPoint.position); //attempt to stop move ***NOT WORKING PROPERLY***
         {
             Debug.Log ("Stopped");
             shouldMove = false;
         }
     }
 
     void checkpos(){ //attempt to store orignal starting position
         Debug.Log ("Checking");
         pos0 = startPoint.localPosition;
     }
 
 }


EDIT: Adding a Debug.Log to also print the pos0 co-ordinates and the transform.position co-ordinates, it is detecting the differenec (i.e transform.position is NOT equal to pos0 - yet it changes the bool from true to false after 1 frame anyway!

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 Baste · Nov 11, 2015 at 02:35 PM

It's because you have a ; at the end of the if-check.

So this:

 if(transform.position == endPoint.position);
 {
      Debug.Log ("Stopped");
      shouldMove = false;
  }

Should be this:

 if(transform.position == endPoint.position)
 {
      Debug.Log ("Stopped");
      shouldMove = false;
  }

There's two somewhat strange C# things going on here. First of all, ; is a valid empty statement on it's own. At the same time, you can wrap any set of statements in { }, which creates a local scope. You usually only do it to define what an if- or for-statement should work on, but it's completely valid to just do it whenever. So your code is doing this:

 if(transform.position == endPoint.position)
     ; //Do nothing
 { // Bracket does not interact with if-statement
     Debug.Log ("Stopped");
     shouldMove = false;
 } 

Dropping the ';' should fix your problem.

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 jenniasanders · Nov 11, 2015 at 05:46 PM 0
Share

Thankyou very much @Baste ! It's always something so small.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

i tried to make an if statement that 2 gameobjects are drop in a same position to enable another gameobject but it's not working 0 Answers

SetActive() not working on pooled objects? 2 Answers

When Time.timeScale go from 1 to 0 Unity destroy a gameobject 1 Answer

clone a game object and use it 0 Answers

The Object you want to instantiate is null. 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