- Home /
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.:)
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.
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?
sorry on delay, lemme play with the code a bit tomorrow i'll try to have a response back to that.
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!)
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.
Glad you got it working! Best of luck with your game
Your answer
Follow this Question
Related Questions
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
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
How to make a object move right automatically in 2d. 2 Answers