I’m working on the mechanics for throwing a ball but can’t seem to get it just right and am not sure why. I’m using the projectile movement component in order to move the ball.
Basically, I spawn in my ball which contains a sphere collision component, static mesh, and projectile movement component. I set the projectile movement component to be deactivated on spawn because I want the object to be thrown once a character picks it up, not directly from the body like a bullet.
I know that projectile movement component and simulating physics are not compatible. However, when my ball first spawns, the physics are enabled so that it doesn’t just float there. Then, when my character overlaps with the sphere component, the ball is attached to my characters hand socket and I disable physics. Finally, once I left click, the ball is detached, simulation for the projectile movement component is enabled, initial speed is set, etc - and the ball is “thrown”.
The problem is, when the ball is thrown it doesn’t collide with any of the walls on the level, or the ground. Here’s the screenshots of my code:
I hope that makes sense. Any advice would be greatly appreciated, thank you!
If anyone comes across this thread, I solved it myself eventually. Basically - a very simple mistake. If you look closely at the OnOverlapBegin function, you’ll see that I turned collision off. Now you might be thinking, well duh idiot, the problem is collision and it’s off?
My thought process was that in order to attach to the socket I would disable everything, and then I would turn it back on right after the ball was detached and launched. Obviously, this was wrong. Instead, what needs to be done in OnOverlapBegin is you need to disable physics for the SphereComponent but keep your collision. Then, the way I did everything else for the ball being thrown was correct, with Activate() being called after you set your variables.
One last thing! The way I am detaching in the above pictures is incorrect. For one, I never set what object I was detaching, you can see I call the function in the scope of the GymCharacter so one could assume I’m trying to detach something from the character here. The fix to this is to call DetachFromActor from the Dodgeball component. Additionally, the detach needs to be called AFTER the Activate() function.
Hope that helps anyone that’s lurking in the future.