transform.position messing up Linecast

I have an object that I want to keep on the ground through Linecasts rather than gravity (currently just have one Linecast for simplicity). The Linecast goes from the center of the object, to an empty child which is positioned just below the parent’s base (so it always hits the ground). Whenever the Linecast hits, the object’s Y position is changed to the Linecast’s point of contact with the ground (+1 unit for accommodate for the object Y scale).

If the object is stationary everything is fine, same if I move it sideways (on X and Z axis). But whenever there’s upward movement, whether through a slope or me just dragging the object using the Inspector window, the Linecast starts vibrating vertically. This will continue to happen until I detach the object from the ground and place it back on a flat surface. When detaching it, either via inspector upwards, or moving it off the edge of another object, there’s a sudden small launch upwards.

This also happens when I spam the jump key, and when the object jumps, it goes extra high (even when jumping from a flat surface).

Note, this only happens if the Linecast is vibrating.

Here’s the code for the Linecast (and the code I believe is causing the issue).

RaycastHit hitInfo;

if (Physics.Linecast(transform.position, bottomObject.position, out hitInfo) && hitInfo.collider.tag != "Char") {
    Debug.DrawLine(transform.position, bottomObject.position, Color.green);
    transform.position = new Vector3(transform.position.x, hitInfo.point.y + 1f, transform.position.z);
}

else Debug.DrawLine(transform.position, bottomObject.position, Color.red);

After days of testing and changing code back and forth, I narrowed it down to being the “transform.position”.

I made two debug prints, one showing the position of the Linecast impact point, and the other showing the position of the object’s empty child. Though it didn’t really matter what the second debug printed, and here’s why. Whatever part of the object (either it or its children) I printed the position of, the Y part would always jump up by 0.1, and then back down by 0.1 (meaning it was the whole object being moved, not just a child). This happens 2-3 times a second whenever the Linecast is shaking.

I’m running the Linecast code in Update, and anything that changes forces, velocity, etc, in FixedUpdate. And since I know now what the issue is, I won’t post the full script (unless someone requests more code).

What can I do to stop this? I read somewhere on an unrelated problem’s thread that shaking could be the transform.position and rigidbody (I have a rigidbody to add my own gravity at specific points) fighting over object placement, since maybe the object is being teleported into geometry, but this is happening even when I add more than 1 to the Vector3 line’s Y change, and the object is constantly floating.

From what you posted it seems like you are having sync issues from your manual positioning calculation and the physics calculation.
I can’t say this will solve your issue, but try doing all your calculations on the same frame (even better on the same stack) (not depending on coroutine, invoke, etc).
Ideally I’d do all your movement calculations on FixedUpdate, because you use physics as well. But if it doesn’t work as desired you could try LateUpdate.
This could also be some helpful reading: Unity - Manual: Order of execution for event functions

I forgot to mention this in the original post. Whenever I move the Linecast stuff into FixedUpdate, the object adopts the exact same violent vertical shaking as the Linecast.

I tried putting all movement stuff into FixedUpdate, then into Update, and then into LateUpdate. Nothing worked so far.

Is your foot object affected by physics collisions? Could it be that what’s causing it to shake? Make sure you check tick/untick the collisions map on the project, 2D and 3D, depending on the obj type.

No, the foot object only has the Transform component. I also unchecked the collision matrix so the “Char” (which the main object and its children have assigned) layer can’t collide with itself, but the shaking is still happening.

I also tried using a Raycast instead of a Linecast, and giving it the same length (by first debug logging the distance between the parent and the foot object), but that didn’t change anything.

I also just discovered something odd. I have a slope connected to a flat cuboid. If I walk onto the cuboid via the slope, and while the ray/line is shaking, I can make the main (parent) object jump by shaking my mouse left/right. I disabled my camera controller script (which orbits the camera using the mouse), and it still happens. The object has absolutely no code that checks for, or uses any mouse input.

I’ve been swinging my cursor left/right at different speeds, angles, for about 10 minutes before deciding it’s not coincidence. If I don’t touch anything and let the object sit there, nothing happens. As soon as I fling the cursor either left or right, the object will (seemingly at random) jump slightly. This is the same jump behavior that happens when walking off edges while the ray/line is shaking.

If I use:

Cursor.lockState = CursorLockMode.Locked;

This doesn’t happen.

P.S. The object is 3D.

Seems like you’re getting some undesired effects from other objects actions.
My first guess would be that the mouse is moving the camera and the camera is affecting your gameplay.
First I’d make sure the camera doesn’t affect in any way your player, check if your ray/line casts don’t use the camera in any way, or that the camera doesn’t have collisions.

Then I’d segregate your issue into a separate scene, having as minimal number of objects as possible to test the player jumping.

Another thing I would do as well is to spread several Debug.Logs/print across the scripts in key points with specific texts, like “press jump”, “camera moved by mouse”, “camera moved by player movement”, etc… Doing that and monitoring the console will help you understand what part of the code is being triggered and perhaps find out if something is being executed (and the order) when it shouldn’t.

Lastly, if you can’ find out share some of the code with us for further insight.

The object collider is affecting it somewhat. I currently have a capsule collider btw.

If I disable the collider or move its center Y so that it’s above the object, the shaking is super violent while on flat ground, if I press the jump key there is almost no shaking, if I press it again, the object jumps and the line is violently shaking again once the object lands. It also increases the shaking when climbing the slope (changing Y position).

So the collider is affecting it, in that it’s dampening the shaking when active. So on its own, the linecast (which I have now changed to a raycast with a downwards direction, and removed the feet object) has some very strange and strong shaking.

Hopefully since I now know how the raycast acts on its own, I’ll be able to figure out a solution, but right now my mind is drawing a blank.

Also, there’s a line of code for setting the object’s velocity (movement), and in it I’m setting the Y velocity to its current velocity, so that input from the object doesn’t make it move up or down.

playerRigidbody.velocity = new Vector3(movementVelocity.x, playerRigidbody.velocity.y, movementVelocity.z);

If I set that to Y velocity to 0 instead, there is no shaking even when standing on the slope. But every time the object moves, the shaking will happen until the object stops (when player input stops).

So that piece of code is also affecting the shaking it seems.

Thanks for the info, 2 advises I can give you from what I read, first try not having any secondary colliders attached to your character(for testing this issue at least) and use this instead, to check collision with other objects:

Also, make sure your collision 2d and 3d maps are set correctly that what should and should not collide.

Second, instead of adding velocity, add force instead. Adding velocity bypasses some physics calculations and can cause undesired effects.

Shaking still happens when:

  • Adding Force while using only the OverlapBox.

  • Adding Force while using only the Capsule Collider.

  • Adding Force while using both.

  • Adding Velocity while using only OverlapBox.

  • Adding Velocity while using only the Capsule Collider.

  • Adding Velocity while using both.

Here’s my Physics settings:

I tried messing with all of them, so far got nothing. I reset the settings afterwards and just changed the collision matrix (just in case I left some value altered).

The “Default Contact Offset” tooltip says if the value is too close to 0, it could cause jiterring, but I tried increasing it (and decreasing it), but nothing.

At this point, after using OverlapBox, I leads me to believe the jitter might not be caused by collisions only. I’d suggest starting that object movement script from scratch and test on every little step of the implementation to check what is causing the shaking.

You can give us the code afterwards to analyze.

Your not auto syncing your transform and rigidbodies, so that could be an issue. Either tick the auto sync setting on that dialog you screenshotted, or sync it manually yourself at the points required. It could otherwise cause some discrepency between where the rigidbody is and transform.

I have no idea if this is what is causing the issue but its worth investigating if you have no idea what is causing it.

EDIT: Also how big are the values, what position in the scene is the object. You could be experiencing floating point precision issues which are known to cause jitter. The bigger your floats the less precise they become, and for position this creates noticeable jitter.

When I debug the change, it’s “0.1” but in the inspector it’s “0.000001”. The object starts at the center of the scene with a 1.5 Y position to accommodate for its height.

Checking “Auto sync transforms” didn’t change anything.

Yeah. I found a tutorial yesterday that’s pretty recent, which goes up and beyond what I have right now, and it handles uneven terrain and slopes beautifully. So I might just go through that and try to incorporate as much as possible into my script.

Hopefully that will work, but if it doesn’t I will post my original controller script in full. If I get issues while working on the tutorial script, I’ll probably post it to a new thread and link it here (to keep things on topic).

The tutorial script works fine, except for the fact that it’s a trade-off between shaking and my character floating/sinking slightly.

I’ll just try working from my original script, and changing the method of relocating the player. If anyone has any further suggestions or ideas on how to fix this issue, feel free to reply here, but I’ll be moving on from this issue for now.