As part of a project I'm doing, I need to model a ball with three accelerometers attached to it in Unity. To do this I've created a sphere that can be controlled through keyboard input, and attached three very small cubes to it at different points. I need to collect the positions and angular velocities of all four objects. Collecting the angular velocities require me to attach a rigid body component to each object, which prevents me from making the cubes children of the sphere. Now because I can't utilize the parent-child relationship I've had to attach a hinge component to each of the three cubes and used them to link the cubes to the sphere. My issue now is this. I want to also collect the location of the cubes with respect to the spheres local coordinates at a given sampling rate. I expected this to be a constant value for every sample since the sphere and cubes move together simultaneously and so, from the perspective of the sphere, the cubes are not changing position at all. But for some reason I get the positions of the cubes with respect to the spheres local coordinates to be a changing quantity. Why is that? Have I misunderstood something about this situation or is there an issue with my code, given below? Is it because I attached the cubes to the sphere using a hinge and so the cubes are not truly fixed, i.e. have small rotations/movements around the placement point? I'm truly at a loss here and I appreciate any help you can offer. Even just pointing me in the right direction would be great. Thanks in advance!
// "other" is a GameComponent that refers to the sphere object
// This script is attached to a single cube to transform.position refers to one cube
Vector3 d = transform.position - other.transform.position;
Vector3 rel = Vector3.zero;
rel.x = Vector3.Dot(d, other.transform.right.normalized);
rel.y = Vector3.Dot(d, other.transform.up.normalized);
rel.z = Vector3.Dot(d, other.transform.forward.normalized);
com.Add(rel); // com is a List that holds all the sampled positions
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
361 People are following this question.
Unable to Add script to an object
It does not work removing items from the inventory
need this script to add cash instead of subtract and take away cash
NullReferenceException in FiniteStateMachine with ThirdPersonCharacter
Trying to have bullets travel in the direction the player is facing