- Home /
Catapult question
I'm trying to make a catpult-based game. I made a model in blender, and imported it. I added a rigidbody to the catapult (so that it can fire a rigidbody projectile.) I also added appropriate mesh colliders. I now have two questions: 1. How do I stop the catapult just falling through my terrain (with terrain or box collider - neither seem to stop it.) 2. How would I rotate the catapult? This is porbably harder as I want to rotate it from one end, rather than from the middle. I think I need to use a hinge joint, but I can't get this to work either.
Answer by Chris D · Mar 04, 2011 at 11:07 PM
As for your first part, check to see where your collider actually is. Is it encompassing the catapult properly? Is it connected properly?
For the second, you could use the RotateAround function as follows
var hingePoint : Transform; var rotateSpeed : int;
function Update() { // Spin the object around the hinge origin at rotateSpeed degrees/second. transform.RotateAround (hingePoint.transform, Vector3.forward, rotateSpeed * Time.deltaTime); }
http://unity3d.com/support/documentation/ScriptReference/Transform.RotateAround.html
In my code above, you would place an empty gameobject (containing only a transform) at your hinge point. The arm would then be rotated around that point at your chosen speed.
Alternatively, you could use something like Rigidbody.AddForce or whichever of the functions listed here suits the type of movement you're after best.
I don't have much experience with hinge joints, so I can't speak to that unfortunately, but it sounds like that's pretty much what they're designed for.
While that would rotate your transform, you would have to use a physics based rotation method if you wanted to bring the projectile along with it. $$anonymous$$anipulating the Transform directly ignores physics calculations. You should you methods belonging to Rigidbody such as AddTorque() or using a HingeJoint.
I was planning on firing the projectile using physics. But now I'm just doing it all via an animation. Thanks for the help though.
Answer by owenpat · Mar 05, 2011 at 06:37 AM
The catapult doesn't need a rigidbody, only the arrows do. The most common way to have it pop back a little after it fires would be to play an animation.
For me, the most common cause of a collider falling through the ground is having the bounding area too far below the ground. When two solids are "in" each other, instead of locking them up (like being trapped in a wall) Unity lets them pass through until they are apart. To test, lift it up and see if it falls and stops on the ground.
The most common way of rotating around a certain point is to fix it in the modelling program. Slide all the verts until (0,0,0) is where you want it to rotate around, then reimport. You will probably use a mount point (an empty childed to the object) for the firing position. That will rotate with you just fine, no matter where your center is.
A hinge would also work, but is more for if you want it to flop around sideways as things ram it.
Your answer
Follow this Question
Related Questions
Rigidbody stuck in corners of Mesh Collider 0 Answers
Rigidbody SweepTest doesnt work for mesh collider? 2 Answers
Is there any way to improve collision detection accuracy with the mesh collider? 2 Answers
How to allow collisions on a Child of a Non Kinematic RigidBody ? 1 Answer
Can you scale a Mesh independently of it's Collider? 1 Answer