add overlapBox/Sphere/Capsule function, fixed OnWake/OnSleep not work

This commit is contained in:
2025-12-28 22:40:48 +08:00
parent d0eed3a33d
commit 9a44dd8d79
34 changed files with 1439 additions and 280 deletions

View File

@ -1,4 +1,3 @@
using System;
using Prism;
namespace FPSExample
@ -9,7 +8,7 @@ namespace FPSExample
public float RunSpeed = 20.0F;
public float JumpForce = 50.0F;
public float MouseSensitivity = 10.0F;
public Vec3 Forward = new Vec3(0.0f, 0.0f, 1.0f);
public float Distance = 5.0F;
private bool m_Colliding = false;
private float m_CurrentSpeed;
@ -44,12 +43,11 @@ namespace FPSExample
void OnUpdate(float ts)
{
Forward = m_Transform.Forward;
if (Input.IsKeyPressed(KeyCode.Escape) && Input.GetCursorMode() == Input.CursorMode.Locked)
{
Input.SetCursorMode(Input.CursorMode.Normal);
}
if(Input.IsMouseButtonPressed(Input.MouseButton.Left) && Input.GetCursorMode() == Input.CursorMode.Normal)
Input.SetCursorMode(Input.CursorMode.Locked);
m_CurrentSpeed = Input.IsKeyPressed(KeyCode.LeftControl) ? RunSpeed : WalkingSpeed;
@ -78,9 +76,33 @@ namespace FPSExample
m_LastMousePosition = currentMousePosition;
}
Collider[] colliders = new Collider[10];
private void UpdateMovement()
{
RaycastHit hitInfo;
if (Input.IsKeyPressed(KeyCode.H) &&
Physics.Raycast(m_CameraTransform.Transform.Translation + (m_CameraTransform.Forward * 5.0f),
m_CameraTransform.Forward, 20.0f, out hitInfo))
{
FindEntityByID(hitInfo.EntityID).GetComponent<MeshComponent>().Mesh.GetMaterial(0).Set("u_AlbedoColor", new Vec3(1.0f ,0.0f, 0.0f));
}
if (Input.IsKeyPressed(KeyCode.I))
{
// NOTE: The NonAlloc version of Overlap functions should be used when possible since it doesn't allocate a new array
// whenever you call it. The normal versions allocates a brand new array every time.
int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Transform.Translation, new Vec3(1.0F), colliders);
Console.WriteLine("Colliders: {0}", numColliders);
// When using NonAlloc it's not safe to use a foreach loop since some of the colliders may not exist
for (int i = 0; i < numColliders; i++)
{
Console.WriteLine(colliders[i]);
}
}
if (Input.IsKeyPressed(KeyCode.W))
m_RigidBody.AddForce(m_Transform.Forward * m_CurrentSpeed);
else if (Input.IsKeyPressed(KeyCode.S))
@ -105,7 +127,7 @@ namespace FPSExample
Vec3 translation = m_Transform.Transform.Translation;
cameraTranslation.XZ = translation.XZ;
cameraTranslation.Y = translation.Y + 1.5F;
cameraTransform.Translation = cameraTranslation;
cameraTransform.Translation = cameraTranslation + m_CameraTransform.Forward * -Mathf.Clamp(Distance, 0, 10);
m_CameraTransform.Transform = cameraTransform;
m_CameraTransform.Rotation = new Vec3(m_CameraRotationX, m_RotationY, 0.0f);