- Home /
Moving object along with raycast
I'm a newbie in Unity. I'm working on a small game like Bubble Shooter and I have a question: How can I fire a ball in exactly a ray which I aimed (reflection line as well) like an image below? I can draw a ray to aim but I do not know how to fire ball in a way though I searched a lot of posts on the internet. How do I do this? Thank you so much! I'm looking for your answer!
Answer by Acdia · Aug 16, 2021 at 05:28 PM
Hi! I'm not sure if I understood the part with the reflection line, but to just shoot the ball in the direction you aimed you could do the following: Make a empty GameObject and add your line as a child. Rotate the newly made GameObject in game as you want, instatiate the "projectile" and use AddRelativeForce(transform.up * whatEverYourForceIs);
(assuming you are using 2D and a Rigidbody) on the projectiles Rigidbody to shoot it forward.
Now to the reflection line: If I got it right you just want the ball to bounce off walls? In this case just add colliders to the walls, and assign a PhysicsMaterial to the with bounce = 1 (or whatever you want). I hope I got it right, but since no one seems to give an answer I decided to try it...
Thank you for your response! I will try it now. Btw, I have tried AddForce,Velocity,MoveForward,... and even calculate angle to shoot but nothing works :(
Ok. In case you get any issues, you could provide a picture of what's happening and the code. I can then try help debugging.