• 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 looMeenin · Dec 11, 2014 at 01:57 AM · unity 4.6movetowards

Can't get MoveTowards() to work

I have an 'options' button and a 'back' button and if they are pressed the camera in the scene moves towards a target. When the 'options' button is pressed the camera moves to the desired position but when I press the 'back' button the camera won't move. I can't figure out what's going on.

 public void LateUpdate(){ 
 
         if (optionsButtonPressed == true && backButtonPressed == false) {
 
             // Move the camera's position between it's current position and it's new position.
             newPosition = Mathf.MoveTowards(camera.transform.position.x,-7,Time.deltaTime * 20);
             camera.transform.position = new Vector3(newPosition, 0,-5);
         }
 
         if (backButtonPressed == true && optionsButtonPressed == false ) {
 
             // Check to see that if conditions have been met(they have) 
             print ("yahadad");
 
             // Move the camera's position between it's current position and it's new position.
             newPosition2 = Mathf.MoveTowards(camera.transform.position.x,7,Time.deltaTime * 20);
             camera.transform.position = new Vector3(newPosition2, 0,-5);
         }
     }

...

public void OnMouseUp(){

         if (gameObject == optionsButton) {
             backButtonPressed = false;
             optionsButtonPressed = true;
         }
 
         if (gameObject == backButton) {
 
             optionsButtonPressed = false;
             backButtonPressed = true;
         }

         print ("optionsButtonPressed = " + optionsButtonPressed + "  Camera Position: " + camera.transform.position);
         print ("backButtonPressed = " + backButtonPressed + "  Camera Position: " + camera.transform.position);
 

}

Here is the console output:

// When options button is pressed:

optionsButtonPressed = True Camera Position: (0.0, 0.0, -5.0)

backButtonPressed = False Camera Position: (0.0, 0.0, -5.0)

// When back button is pressed:

optionsButtonPressed = False Camera Position: (-7.0, 0.0, -5.0)

backButtonPressed = True Camera Position: (-7.0, 0.0, -5.0)

yahadad

yahadad

yahadad ...

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 Rohit-M · Dec 11, 2014 at 01:42 PM 0
Share

I can't see any real difference between the two movements. Have you tested to see if you're booleans are being set properly? Are they staying set? What is the cameras position.x during and after movement?

avatar image Volearix · Dec 11, 2014 at 03:13 PM 0
Share

You might try newPosition = $$anonymous$$athf.$$anonymous$$oveTowards(camera.transform.position.x,7,Time.deltaTime * 20); on line 15. I've had some weird issues like this, I'm fairly new to unity, but on my issue it seemed like it was modifying the local area, not the playable map area. Like it's moving 0 world units or whatever.

avatar image looMeenin · Dec 11, 2014 at 08:27 PM 0
Share

Rohit $$anonymous$$ - Yes I checked to see if my booleans are being set and they are. The camera moves to the desired position when I click on the options button but when I click the back button nothing happens, yet the booleans seem correct..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Dec 11, 2014 at 03:17 PM

What is that target you want to reach? If you try to move your camera to another GUI content in your scene, make sure the rendermode of your Canvas is not in overlay or screenspace mode as in this case it doesn't matter where the camera is pointing at. The GUI stuff just moves with it. In overlay and / or screenspace mode you have to move the content inside the canvas. You can use an empty gameobject (with RectTransform) as parent for all GUI stuff and move that inside your Canvas.

edit
Well, just reread your question. If it moves correctly in one case but not the other it's most likely that your other position is wrong (0,0,-5) or that you reset your booleans somewhere else immediately.

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 looMeenin · Dec 11, 2014 at 08:28 PM 0
Share

Bunny83 - I am not using Unity's GUI componenets. I am using sprites with colliders that respond to touch.

I can get print statements to output to console in each of the if statements when the buttons are pressed so the conditions are being met, but it's still not moving when I press the back button. I've updated the question to include print statements and output.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

Simple car script not work on unity 5? 1 Answer

Create animation sequence from different FBX models 1 Answer

Unity 4.6 and up have broken my sprite layers. How to fix? 1 Answer

Rotating Tank Turret with correct angle 3 Answers

Unity iOS App quitting from Xcode because of Unloading x unused Assets to reduce memory. 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