- Home /
,How to change the direction of an already moving GameObject
Suppose a ball is moving on xy plana in the y-direction. I have a box placed in the same xy plane, I want that when I touch the box, the ball instantaneously moves towards the box,no matter what direction it was going.
Someone please help.I'm new to unity.
Answer by YetAnotherKen · Sep 09, 2019 at 08:01 PM
You can do that with scripts. Try out that ball rolling tutorial project to learn the basics you need.
I'll have to study the whole tutorial just to implement one feature!!!!!..
can you please tell me how to do it?
Answer by ZeBarba · Sep 09, 2019 at 08:58 PM
With the information provided, probably use Vector2.MoveTowards(). There is also the same method with Vector3, if needed.
A good video about how to move stuff is: https://www.youtube.com/watch?v=tNtOcDryKv4 It presents 5 ways and the difference in each case.
The problem is that .$$anonymous$$oveTowards uses Update method which is called as soon as the game starts. Rather I want the ball to move on its own path until and unless I touch the box(OR click a button).
In short,I want the update function to not work unless I want it to. If I use enabled=false; then I cant find a way to enable it again using button event ...
Please help.
You need to create a method to detect when you touch the cube, let's say "CubeTouched()". It will act like a trigger. Inside this method you call the $$anonymous$$oveForwar, only after the cube was touched.