I’m very new to Unity, but I do have prior experience with XNA.
My Problem:
I have a target object, lets call it “x”
I have a launcher at Pos (0,50,0)
Now, to shoot from launcher to “x”, I use the following logic -
I get direction, which is: (x.transform.position - Vector3(0,50,0)).normalized
I then apply force in the direction I got
This missile is a Prefab - Imported Mesh + RigidBody + Sphere Collider
The issue is that - the force moves the missile in a different direction (specifically in Z direction)
Things I’ve tried out:
I changed the missile to a Prefab: Sphere + etc etc
The code above works fine
I changed the missile to a Prefab: Cube+ etc etc
The code above works fine
It’s only with prefab that has Imported Mesh.
Other notes:
In the Editor - everything looks centered
I Interpolated the positions values in an excel file and compared it against my Debug.Logs. It’s like only the “Z” axis calculations seem to be wrong, More specifically, it starts of fine and then incremental values are more and missile moves a little more in the z direction than what it actually shud have been
Instead of AddForce I tried .transform.Translate(direction). Even this moves the object more in the Z axis
Puhleeeeeeeeeez help - I cant think of anything else I can do.
I’ve searched this forum and cudn’t related to any post.
transform.Translate will not work well on a non-kinematic rigidbody… When you say it’s moving in the z-axis, do you mean the world or object’s z-axis?
There is a setting on the Rigidbody under constraints that constrains the movement/rotation of the rigidbody to certain axes… maybe one of these was checked by accident?
About the z axis: What I’m logging is transform.position.x, y and z. So I guess this makes it world co-ordinates?
Nope. None of the constraints have been applied. I re-checked.
One doubt though: Rigid body has mass etc, but center is defined in the sphere-collider. Where exactly will the force be applied?
(Since this is like imported mesh)
Are you importing the mesh from a 3d app? Sometimes the Z axis is backward in Unity compared to some 3d applications, for whatever reason. Did you try flipping the object in your 3d app and re-exporting?
AddForce will always be applied at the center of mass, which is calculated by the center of the collider(s) that are attached to the object. There is also rigidbody.AddForceAtPosition which will apply the force at an arbitrary point, which produces some torque on the object if you are not applying the force at the center of mass.
Are you using a mesh-collider on the missile? If so, try setting the mesh-collider to “convex”. You can get some problems with applying forces to non-convex mesh-colliders.
AddForce will always be applied at the center of mass, which is calculated by the center of the collider(s) that are attached to the object. There is also rigidbody.AddForceAtPosition which will apply the force at an arbitrary point, which produces some torque on the object if you are not applying the force at the center of mass.
Are you using a mesh-collider on the missile? If so, try setting the mesh-collider to “convex”. You can get some problems with applying forces to non-convex mesh-colliders.
Here’s a work-around i made -
I created an Empty Game Object. Added RigidBody and Sphere collider to this.
I created an instance of my missile mesh and made it a child of this empty object.
I converted this whole setup into a Prefab.
And Voila!
So my question is - given that this is working - what could I possibly be doing wrong in the initial setup?
Without seeing how you had it setup before it’s hard to tell. It could be that your mesh’s pivot point is not set properly(the pivot point is the local origin of the object, if a vertex is at the object’s pivot point its local coordinates are 0,0,0). To check this set the localPosition of your child missile object to 0,0,0. After doing this if the object is far away from it’s parent transform’s center, then it means you may want to reset the pivot point of your object in 3DS. I don’t use 3DS but I imagine it’s fairly easy to do this, and I do believe it’s called a “pivot point” in that program.
I didn’t know about localPosition and localRotation concept.
So what I did was log these values before and after the setup.
Working set: localPosition is 0,0,0 n pos is 0,0,0
Buggy set: localPosition is 0,50,0 n pos is 0,50,0
Seems fine, al though i’m begging to wonder, if there is a little rotation in model, the local axis cud vary a bit.
I think I shud just conclude something wrong wid the model