Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Poi7ioN · Apr 26, 2019 at 04:34 PM · movementbutton trigger events

Need some help with Object movement on Button press !!.

Need some help implementing movement to a 2d object with ui button events. I have a simple paddle and i want to move it with the help of 2 ui on screen button on which i've used event trigger functions referencing to the paddle script , but i'm missing something stuck on it for a while a little help would be appreciated. this is what i did

 public void MovePaddleRight()
     {
         transform.position = (rigidbody2D.position + new Vector2(10, 0) * Time.deltaTime);
     }
 
     public void MovePaddleLeft()
     {
         transform.position = (rigidbody2D.position + new Vector2(-10, 0) * Time.deltaTime);
     }

the paddle just snaps to the position without and smooth motion. basically all i want is to implement Onbuttondown move to the relative position and onbuttonup stop moving.and vice versa. Please can someone help me out with this i'm kinda stuck on it for a while...thanks in advance.:)

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 surfuay · Apr 26, 2019 at 06:21 PM 0
Share

i think you want translate not position. Been a while since i've looked at that, maybe read through the scripting api on transform.translate which is going to move you in that direction at that amount by your speed * time.deltaTime.

I also think you're missing a speed variable so you would need to set that and then add * speed after your time.deltatime.

avatar image Poi7ioN surfuay · Apr 26, 2019 at 07:30 PM 0
Share

I've tried this rigidbody2D.velocity = new Vector2(dirX * moveSpeed, rigidbody2D.velocity.y); but the problem here i get is the paddle just goes out of bound what should i do about that?

avatar image surfuay Poi7ioN · Apr 28, 2019 at 03:46 AM 0
Share

sorry on delay, lemme play with the code a bit tomorrow i'll try to have a response back to that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Okido · Apr 28, 2019 at 11:13 AM

With moving rigidbodies you want to add force or set velocity (because you are dealing with physics) - setting the transform position will just tell a thing where to be in the world without any consideration for physics, so it will ignore collisions.

That said, if you want to get an object to move smoothly from point A to point B look at https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html. The reason your code is making it snap to a new position is because it calls for a change of position in only 1 frame (assuming your method is called OnButtonDown/Up). The stuff in that link will explain how smooth movements via script work.

Alternatively you can animate the paddles which sounds like the simplest solution based on the very limited knowledge I have of your project :p - it's very quick and easy with Unity's built in animation. This video alone will tell you everything you need to know about setting up animation on an object https://unity3d.com/learn/tutorials/topics/animation/animation-view?playlist=17099. You can then call animations from your methods (Unity's website and Youtube have a lot of tutorials on this - if you want to go that route and want more info let me know!)

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 Poi7ioN · Apr 30, 2019 at 01:58 PM 1
Share

Yes, the problem was persisting due to the standard assets crossplatform imported with my mathf.clamp which i almost forgot about :p, so after commenting that out and using addforce i was able to achieve my task. But unfortunately i'm never content with that so I decided to use a simple slider and everything works fine including the smooth transition and clamping the paddle with $$anonymous$$ and max slider value .I'll try looking into animation furthermore. Thanks a lot for the support, I truly appreciate it.

avatar image Okido Poi7ioN · May 11, 2019 at 10:03 PM 0
Share

Glad you got it working! Best of luck with your game

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

160 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI button adding existing scrip to move 2d character 0 Answers

Moving an Object to the vector of other objects on button press using a vector3 array? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Change Camera Distance of Cinemachine(VirtualCamera)in script 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