Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Count Swagula · May 13, 2014 at 09:59 PM · 2dguibuttonplayerlevel

Moving 2D Game Object on GUI Button Click

Hey guys, I need to move an object to the left when a button is clicked, changing the scene when it goes past a certain point. I want it to move steadily in a direction though instead of teleporting like it is now. Currently when I push the button my 2D player sprite just moves a few units to the right

 void OnMouseDown(){
         Debug.Log("Pushed");
         if(currMenu == menuState.title)
         {
             currMenu = menuState.weapon;
         }
         else if(currMenu == menuState.weapon){
 
             objToMove.rigidbody2D.AddForce(Vector3.right * 3000);
             if (objToMove.transform.position.x > 15){
             Application.LoadLevel("Level1");
             }
         }
     }
Comment
Add comment · Show 2
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 Pyrian · May 13, 2014 at 11:20 PM 0
Share

How odd. AddForce is not a teleport sort of movement. Perhaps something else is preventing your object from continuing to move. Offhand, I would guess there's an animation with apply root motion enabled?

avatar image Count Swagula · May 13, 2014 at 11:27 PM 0
Share

nope, apply root is not enabled

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MikeNewall · May 13, 2014 at 11:21 PM

OnMouseDown is only called once when you click. Use OnMouseOver which is called while your mouse is over the button. You can then check if the left mouse button is down and if so move your object. Something like this:

 void OnMouseOver(){
     if(Input.GetMouseButton(0)){
         // move the player
     }
 }
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 Count Swagula · May 13, 2014 at 11:57 PM 0
Share

Thanks $$anonymous$$ike

avatar image Count Swagula · May 13, 2014 at 11:59 PM 0
Share

Ok guys, I did it by implementing $$anonymous$$ike's suggestion in addition to adding a downward force so the object moves to the side and towards the center of the screen. Thanks!

avatar image
0

Answer by AdvsNoob · May 13, 2014 at 11:42 PM

OnMouseOver will resolve this issue for you, Mike is correct.

 void OnMouseOver(){
   if(Input.GetMouseButton(0)){
     if(currMenu == menuState.title)
     {
       currMenu = menuState.weapon;
     }
     else if(currMenu == menuState.weapon){
       objToMove.rigidbody2D.AddForce(Vector3.right * 3000);
       
       if (objToMove.transform.position.x > 15){
         Application.LoadLevel("Level1");
       }
     }
   }
 }
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 Count Swagula · May 13, 2014 at 11:56 PM 0
Share

Yeah it really helped thanks

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

23 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

Related Questions

[Button with animator] vs. [Sprite with mousedetection] 0 Answers

making and integrating Visual novel element in 3d game? 2 Answers

How to make a script for "jetpack" boost animation 1 Answer

How if you click on the button to open a menu and if you save the list, it closes and saves as a new button with a new list and next to it appears the add button like google chrome 0 Answers

GUI.Textfield backspace control + Button Style problem 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