better cam controls

This commit is contained in:
2026-07-21 21:13:43 -05:00
parent 0063735802
commit fc78d599a8
3 changed files with 216 additions and 43 deletions
+46 -40
View File
@@ -149,17 +149,9 @@ void ApplyWalk()
verticalMovement = Input.GetAxisRaw("Vertical");
horizontalMovement = Input.GetAxisRaw("Horizontal");
}
Vector3 input = Vector3.ClampMagnitude(new Vector3(horizontalMovement, 0f, verticalMovement), 1f);
moveDir = new Vector3(horizontalMovement, 0, verticalMovement).normalized;
if (cameraController != null)
{
// re-matrix the rotation direction so movement is consistent no matter what
// the camera rotation is
Matrix4x4 matrix = Matrix4x4.Rotate(Quaternion.Euler(0, cameraController.playerCamHome.rotation.eulerAngles.y, 0));
moveDir = matrix.MultiplyPoint3x4(moveDir);
}
moveDir = cameraController.Forward * input.z + cameraController.Right * input.x;
}
/// <summary>
@@ -244,44 +236,58 @@ void RotatePlayerTowardMouse()
}
}*/
Camera cam = cameraController.MainCamera;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
Vector3 targetPoint;
// 1. Try real world hit first (most stable)
if (Physics.Raycast(ray, out RaycastHit hit, 1000f))
// When rotating the cam with right mouse, look forward
if (Input.GetMouseButton(1))
{
targetPoint = hit.point;
Quaternion targetRotation = Quaternion.LookRotation(cameraController.Forward);
DougBody.transform.rotation = Quaternion.Slerp(
DougBody.transform.rotation,
targetRotation,
rotationSpeed * Time.deltaTime
);
}
else
{
// 2. Fallback to plane at player height
Plane plane = new Plane(Vector3.up, DougBody.transform.position);
Camera cam = cameraController.MainCamera;
if (!plane.Raycast(ray, out float enter))
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
Vector3 targetPoint;
// 1. Try real world hit first (most stable)
if (Physics.Raycast(ray, out RaycastHit hit, 1000f))
{
targetPoint = hit.point;
}
else
{
// 2. Fallback to plane at player height
Plane plane = new Plane(Vector3.up, DougBody.transform.position);
if (!plane.Raycast(ray, out float enter))
return;
targetPoint = ray.GetPoint(enter);
}
// 3. Flatten direction so we only rotate on Y axis
Vector3 direction = targetPoint - DougBody.transform.position;
direction.y = 0f;
if (direction.sqrMagnitude < 0.001f)
return;
targetPoint = ray.GetPoint(enter);
// 4. Convert to rotation
Quaternion targetRotation = Quaternion.LookRotation(direction.normalized);
// 5. Smooth rotation
DougBody.transform.rotation = Quaternion.Slerp(
DougBody.transform.rotation,
targetRotation,
rotationSpeed * Time.deltaTime
);
}
// 3. Flatten direction so we only rotate on Y axis
Vector3 direction = targetPoint - DougBody.transform.position;
direction.y = 0f;
if (direction.sqrMagnitude < 0.001f)
return;
// 4. Convert to rotation
Quaternion targetRotation = Quaternion.LookRotation(direction.normalized);
// 5. Smooth rotation
DougBody.transform.rotation = Quaternion.Slerp(
DougBody.transform.rotation,
targetRotation,
rotationSpeed * Time.deltaTime
);
}
/// <summary>