Vector3 multiplied by its magnitude results in infinity?

I’m working on a section of code that needs to find a vector from the difference between two other vectors:

Vector3 displacement = (attachedTransform.position - masterTransform.position);

Then the code needs to multiply this vector (displacement) by it’s own magnitude:

displacement *= displacement.magnitude;

Which is where things go wrong. Unity handles the vector fine until I try to apply it to a transform. At this point, it gives one of several error messages:

transform.position assign attempt for 'Rope.Node' is not valid. Input position is { NaN, NaN, NaN }.


transform.position assign attempt for 'Rope.Node' is not valid. Input position is { infinity, infinity, infinity}.

Note that the infinity error usually states either one or two of the components as being negative infinity.

I’ve used monoDevelop debugging and in the method that is supposed to return this Vector3, MonoDevelop has no problem showing me values for displacement, displacement.magnitude, or displacement.sqrMagnitude; they are all perfectly fine values, e.g.

displacement.magnitude = 3.208273
displacement = {(-0.7,0.6,0.3)}

I’m a little lost as to what is causing this, has anyone else seen this?


Let me explain a little better. I’ve designing a system that contains a bunch of instances of a class called Node. Nodes have a List of another class, called Target. Each frame, the Node runs through the list of targets and adds up each one’s displacement (kind of a force, if you will) on it’s master node.

In this case, the system is composed of a string of nodes where each node has a target on the previous and next nodes, so that each node is pulled towards both of its neighbors. I want this displacement to be the squared distance between the nodes, which is why I am NOT normalizing displacement before multiplying it again by it’s magnitude.

I have also tried:

Vector3 displacement = (attachedTransform.position - masterTransform.position);
Vector3 n = displacement.normalized;
float d = displacement.sqrMagnitude;

return n * d;

and it results in the same error.

Here is the more complete code, for reference:


        public Vector3 getDisplacement() {
                Vector3 n = (attachedTransform.position - masterTransform.position);
                float d = n.sqrMagnitude;
                //weight is a float from 0 to 1 that allows balancing of targets
                return n*d*weight;


       public void Update() {
            Vector3 displacement =;
            foreach (Target t in targetList) {
                displacement += t.getDisplacement();
           //     Debug.DrawLine(transform.position, transform.position + t.getDisplacement(), c);

            RaycastHit rayHit;
            if (Physics.Raycast(transform.position, displacement.normalized, out rayHit, displacement.magnitude, collidesWith)) {
                //if we hit something with our displacement, only move to that point
                transform.position += displacement.normalized * (rayHit.distance - colliderRadius);
            } else {
                //if we didn't hit something, apply full displacement
                transform.position += displacement;

You’re not using a normalized vector…
so your first and second lines result in an exponential displacement, which, if applied every frame will keep pushing the displacement further and further out exponentially.

Technically you only need the first line.

Get rid of “displacement *= displacement.magnitude;”
So just have “Vector3 displacement = (attachedTransform.position - masterTransform.position);” and see if that works.


Try the following instead.(though my first recommendation is a shorter simpler version of achieving the same…this will just explain what is missing.)

Vector3 displacement = (attachedTransform.position - masterTransform.position);

Vector3 direction = displacement.normalized;

displacement = direction * displacement.magnitude;