- Home /
Throw ball with Cannon Physics
Hellow,
I'm trying to make a Canon-Type launcher.
So I need to add force to a Ball (Sphere) from Box Collider situated inside the Cannon.
So far what I've been trying, is to Add Force to the Ball relative to the Cannon (Box Collider) angle and direction (based on his X axis).
Edit: Maybe I wasn't clear ennough, the force need to be applid to an already existing ball.
Answer by skovacs1 · Oct 12, 2010 at 07:03 PM
What part is not working exactly?
Here is a simple script that does what you describe:
Cannon.js
var cannonball : Transform;
function Update() { if(Input.GetButtonUp("Fire1")) { var projectile = Instantiate(cannonball, transform.position, transform.rotation); projectile.rigidbody.AddForce(transform.right * 10000);//cannon's x axis Physics.IgnoreCollision(projectile.collider, collider); } }
The Physics.IgnoreCollisions is there to prevent unwanted collisions between the projectile and the cannon. In stead of Physics.IgnoreCollisions, you could have no collider on the cannon or you could have a mesh collider on the cannon whose shape you know will not inadvertently collide with the projectiles or you could simply instantiate the projectile away from the cannon slightly.
It was for an already existing ball, as to simulate an explosion that add force.
IE: Another example would be a slingshot. Except I don't want to use colliding physics.
Anyway, I modified the script a bit inspited from what you've given me and will make it active only while the sphere is inside the "Explosive Zone" of the cannon.
You can achieve that with something like cannonball.rigidbody.AddExplosiveForce(10000, transform.position - transform.right, radius);
Your answer
Follow this Question
Related Questions
Hovercraft Physics Problems 1 Answer
Making a paper like object on Unity. 2 Answers
Returning a rigidbody back to its original x and z rotations through physics forces. 2 Answers
Make object stick to the ground in 2D 2 Answers
ForceMode.Acceleration estimated dst != covered dst with a single Addforce in effect 1 Answer