• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Strange Positions update in FixedUpdate()


New Coder
I'm trying to addForceAtPosition at the position of a triangle of the mesh of my moving rigidBody object.

At Start() I'm storing the mesh information I need in multiple Arrays like the local positions, the surfaces or the normals of the triangles of the mesh.
As trianglesPositions are local values, I get the WorldTrianglePosition by adding the localPos to the mesh WorldPos (the transform.TransformPoint() solution in FixedUpdate was giving me wrong results and wasn't helping with my actual issue).

There is my problem:
Trying the script in FixedUpdate, as the object is moving (here it's falling), the position is "offseting" more and more with time.
In Update the same code kind of works but when I try to DrawRay from this position, the ray is flickering. And since I'm adding Forces I want to be able to do that in FixedUpdate.

Also, the force I add to the object is relative to the triangle speed. I do that by comparing the actual triangle position from it's previews position. I think that the flickering thing I get in Update come from this as the object moves with physics. I don't get the flickering doing the calcul in fixedUpdate.

I've tried many differents things but nothing seams to work consitently. I Even tried to move an object at the position simply by doing:
theSphere.transfom.position = theObject.transfom.position;
but again doing this in FixedUpdate it doesn't work, the sphere is "in late" it's like a linear lerp from it's position to thePosition I want it to be.

Here is a part of my script and two screenShots.

What do I don't understand here ?

Thank you for your time :) let me know if you need more information !

// For Each triangles, get Angle difference between normal and velocity directions
            for (int i = 1; i < trianglesPos.Length; i++)        //trianglesPos.Length
                //Get the direction of triangles movement and their speed
                Vector3 triangleDirecion;
                float triangleVelocity, normAngle;

                triangleDirecion = ((rb_Mesh.transform.position + trianglesPos[i]) - trianglesWorldPos[i]).normalized;
                triangleVelocity = Vector3.Distance((rb_Mesh.transform.position + trianglesPos[i]), trianglesWorldPos[i]) / Time.fixedDeltaTime;

                trianglesWorldPos[i] = (rb_Mesh.transform.position + trianglesPos[i]);

                // Get the angle between direction and triangleNormal
                normAngle = Vector3.Angle(transform.TransformDirection(trianglesNorm[i]), triangleDirecion);
                normAngle = Mathf.InverseLerp(90f, 0f, normAngle);           //translate normAngle as a multiplier, 90 to 0 = 0 to 1, more than 90 is 0

                //Debug.Log("triangleSpeed is: " + triangleVelocity);
                Debug.DrawRay(transform.TransformPoint(trianglesPos[i]), transform.TransformDirection(trianglesNorm[i]), Color.red);
                Debug.DrawRay(transform.TransformPoint(trianglesPos[i]), triangleDirecion * 2f, Color.green);

                // if Angle difference is not more than 90 degres, Set the values and apply Force to the triangle in his oposite normal direction
                if (normAngle > 0f && triangleVelocity != 0f)
                    // Set values  > Get Triangle Velocity / Normalize Normal Direction / Set aeroForce
                    trianglesNorm[i] = trianglesNorm[i].normalized;
                    aeroForce = Portance * trianglesSurf[i] * normAngle * triangleVelocity;

                    // Add Force
                    if (aeroForce < -0.1f || aeroForce > 0.1f)
                        rb_Mesh.AddForceAtPosition(transform.TransformVector(-trianglesNorm[i]) * aeroForce, transform.TransformPoint(trianglesPos[i]), ForceMode.Force);
                        Debug.DrawRay(transform.TransformPoint(trianglesPos[i]), transform.TransformDirection(trianglesNorm[i]) * aeroForce, Color.blue);


Last edited by a moderator: