Hey,
this is so strange that I don’t even know where to start.
I just updated from 2021 to 2022 LTS and my input is bugged since then.
I use a raycast to determine the position of my mouse on a 3D plane and place an object there. Since the update to 2023 this behaves very strange in a subroutine and snaps to a place either horizontally or vertically when I am dragging my mouse.
But here the part I don’t understand: this only happens, if I play with only the game window visible in the editor or in the build.
It does NOT happen, when I am in the editor and the scene view is visible besides the game view. Then all is fine.
Does that make any sense for anyone?
Thanks
Alex
Before I do that something I just noticed:
The placement function so far was called from within a regular Update(). If I change that to a FixedUpdate() it seems to work.
Nothing in there is relying on physics calculations from outside the function, so I don’t understand why this would be needed or why it worked before. Something changed in that section in 2022 LTS?
Edit:
Keeping the original Update() and adding a debug code that gets printed every frame changes the behaviour as well to the positive… what is happening? I am not sure anymore that the FixedUpdate() really fixes the problem or only masks it, since it just slows down the execution like the print statement and the game view/scene view topic.
My guess is you are positioning some object with a collider in Update and expecting Physics.Raycast to interact with it immediately.
In reality Unity only updates physics colliders in the physics engine during the physics simulation which coincides with FixedUpdate.
Hence why I asked for the code. Two things here:
-
Since your goal is “I use a raycast to determine the position of my mouse on a 3D plane” have you considered using Plane and Plane.Raycast instead of a Physics Raycast
-
There is always Pysics.SyncTransforms if you insist on using the physics engine in Update
My main question up front: If I would do something like you said: why would it have worked perfectly in 2021 but not 2022?
I place an object at the ray intersection and rotate it to look at the mouse as it is moved.
A plane raycast would not be enough, since I interact with other object types as well.
Source code in a sec. Need to sanitize it a bit.
Please never mind some of the spaghetti code. I swear I will fix it 
The Main functions are InteractWithPlayfield() and ChangeByMouse()
public class CustomAngleCursor : CursorAction
{
GameObject colliderInUse = default;
// gets called in Update()
public override void InteractWithPlayfield()
{
if (colliderInUse != null)
{
if (Input.GetMouseButtonUp(0) == true || AppFocus == false || colliderInUse.GetComponent<StoreCollision>().GetCollision() != null)
{
EndInteractiveMode();
}
}
RaycastHit[] hits = SphereCastAllSorted(GetMouseRay(), invalidRadius);
if (hits.Length == 0) { return; }
if (colliderInUse == null)
{
InactiveInteraction(hits);
}
else
{
ActiveInteraction(hits);
}
foreach (RaycastHit hit in hits)
{
if (hit.transform.CompareTag("GamePlane"))
{
SetCursorPosition(hit.point);
}
}
}
private void ActiveInteraction(RaycastHit[] hits)
{
MouseInteraction(hits[0]);
DeactivateCursor();
UseActiveCursor(true);
}
private void InactiveInteraction(RaycastHit[] hits)
{
bool gamePlaneIsFirstHit = hits[0].transform.CompareTag("GamePlane");
if (gamePlaneIsFirstHit)
{
MouseInteraction(hits[0]);
}
UseActiveCursor(false);
MarkCursorFunctional(gamePlaneIsFirstHit);
}
public void UseActiveCursor(bool useActive)
{
activeCursor.SetActive(useActive);
}
private void MouseInteraction(RaycastHit hit)
{
if (Input.GetMouseButtonDown(0) && colliderInUse == null)
{
colliderInUse = Instantiate(GetGameCursor().GetCursorCollider(), hit.point, Quaternion.identity);
colliderInUse.transform.parent = GetInstanceParent();
colliderInUse.GetComponent<CustomAngleColliderFX>().SetValues(amplitude, maxAmplitude, fxDuration, minLength, maxLength);
fxMaterial = colliderInUse.GetComponent<CustomAngleColliderFX>().GetFxMaterial();
}
if (Input.GetMouseButton(0) && colliderInUse != null)
{
ChangeByMouse();
}
}
public override void EndInteractiveMode()
{
if (GetGameCursor().IsSelfDestructing())
{
Destroy(colliderInUse, GetGameCursor().GetSelfDestructTimer());
}
UseActiveCursor(false);
MarkCursorFunctional(true);
colliderInUse = null;
}
private void ChangeByMouse()
{
var mousePos = Input.mousePosition;
var mouseWorldPos = GetMainCamera().ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 0));
mouseWorldPos.y = colliderInUse.transform.position.y;
colliderInUse.transform.LookAt(mouseWorldPos);
int blockerLayerMask = 1 << LayerMask.NameToLayer("Blocker");
bool blockerLimitsLength = Physics.Linecast(colliderInUse.transform.position, mouseWorldPos,out RaycastHit hitInfo, blockerLayerMask);
float calculatedMaxLength = maxLength;
if (blockerLimitsLength == true)
{
calculatedMaxLength = Vector3.Distance(colliderInUse.transform.position, hitInfo.point) ;
if (calculatedMaxLength > maxLength)
{
calculatedMaxLength = maxLength;
}
}
var cursorScaling = Mathf.Clamp(
Vector3.Distance(colliderInUse.transform.position, mouseWorldPos),
minLength,
calculatedMaxLength);
colliderInUse.transform.localScale = new Vector3(
colliderInUse.transform.localScale.x,
colliderInUse.transform.localScale.y,
cursorScaling);
}