I’ve been searching all over, and I haven’t found a solution to the problem I’ve been happening.
I have a camera that follows a player using the standard MouseOrbit script that comes with Unity, and I want to also have a cursor placed on the ground in the game world that corresponds to the cursor’s position on the screen. Right now, I’m using this script:
int floorMask;
void Awake()
{
floorMask = LayerMask.GetMask ("Floor");
}
void Update()
{
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast (camRay, out floorHit, floorMask))
{
renderer.enabled = true;
transform.position = floorHit.point;
}
else
{
renderer.enabled = false;
}
}
This works fine when the player is stationary , but when the player starts moving the cursor will start to jump back and forth between the mouse’s position, and a position behind the camera’s direction of movement, which increases as the speed increases. I’ve tried setting it to run on FixedUpdate() and LateUpdate(), thinking it was just an ordering issue, but that didn’t seem to help, although ill be honest I’m pretty new to this, so I’m not super clear on the difference between these functions.
If anybody knows why this is happening, I’d really appreciate it!
Try printing your floorHit.collider.gameObject.name to see what it’s hitting. My guess is that it’s not what you think it is when it jumps behind the camera.
Well, the ray was hitting other objects in the scene, but Ive managed to solve that bit, so thanks for pointing that out! I realized I needed to have a distance parameter between the layer mask and the previous parameter.
However, unfortunately that doesn’t seem to have solved the jitter problem
Alright, print the hit position and see if that’s also jittering.
If this is to reflect mouse input, by the way, it should probably be done in Update.
A better description of the jitter might also help.
Well, this is a bit odd. I haven’t changed anything as far as I know, but the jitter is way less severe. Before, it would basically lag behind the mouse’s position for a frame, before snapping to the correct position on the next frame. It’s still happening, but rather than shaking across half the screen, it’s just lagging behind the mouse all the time and only vibrating a few pixels, only increasing in severity once the player gets up to speed.
I printed the hit position, although it’s hard to tell if it’s reflecting the issue I’m having. I did however notice that the hit position is only accurate to a single decimal point; could that be the reason, or does the console just round floats like that automatically?
No, the ToString() method for Vector3 only prints one decimal. Internally they’re floats, and if you print the values individually that’s what you’ll see.
So here’s a part of what’s happening with the lagging by a frame, though it may not be the whole thing or the main problem. In the update loop, in separate places, you’re a) moving the camera and b) moving the cursor based on a ray generated via that camera. Depending on what order those things happen in, you may be moving the cursor based on the camera’s position either this frame or last frame.
I’m still not clear enough on the “jitter” to help. Is there a collider on your cursor?
Nope, no collider on my cursor. I wish I could explain it better; it’s basically a vibration back and forth in the opposite direction that the player object, and therefore the camera is moving.
Wait… so the cursor is dependent on the camera, and the camera is dependent on the cursor?
The cursor is just dependent on the camera; the player turns and moves using wasd, and the camera follows it.
I’ve noticed that when the camera is set to FixedUpdate() instead of LateUpdate(), the cursor works perfectly, however the player is the one that now jumps back and forth. Not super useful, but at least it tells me that the problem is in how the camera follows the player.
Hey man, not sure of your whole setup, but does your camera use a rigidbody? Might be interpolation issues? I’m just guessing here… Unity - Scripting API: Rigidbody.interpolation
I feel kinda bad, heh. I’ve managed to solve the issue by just changing the order the scripts are run in the script order settings. Works flawlessly. Thanks for the answers though guys, I’ve noticed that the unity community is way more supportive than a lot of other places ive been.
1 Like
So how exactly did you change the order of the scripts?