add Console panel; fix the problem when cs set Rotation the direction not update; fix some little bug; add simple texture material Edit system;some treaks
This commit is contained in:
@ -8,7 +8,9 @@ namespace FPSExample
|
||||
public float WalkingSpeed = 10.0f;
|
||||
public float RunSpeed = 20.0f;
|
||||
public float JumpForce = 50.0f;
|
||||
|
||||
|
||||
public float m_Radius = 0.5f;
|
||||
public float TorqueStrength = 10.0f;
|
||||
|
||||
[NonSerialized]
|
||||
public float MouseSensitivity = 10.0f;
|
||||
@ -68,15 +70,20 @@ namespace FPSExample
|
||||
UpdateRotation(ts);
|
||||
UpdateCameraTransform();
|
||||
}
|
||||
|
||||
void OnPhysicsUpdate(float fixedTimeStep)
|
||||
{
|
||||
UpdateMovement();
|
||||
}
|
||||
|
||||
private void UpdateRotation(float ts)
|
||||
{
|
||||
Vec2 currentMousePosition = Input.GetMousePosition();
|
||||
Vec2 delta = m_LastMousePosition - currentMousePosition;
|
||||
|
||||
float m_CurrentYMovement = delta.X * MouseSensitivity * ts;
|
||||
m_CurrentYMovement = delta.X * MouseSensitivity * ts;
|
||||
float xRotation = delta.Y * MouseSensitivity * ts;
|
||||
m_RigidBody.Rotate(new Vec3(0.0f, m_CurrentYMovement, 0.0f));
|
||||
// m_RigidBody.Rotate(new Vec3(0.0f, m_CurrentYMovement, 0.0f));
|
||||
|
||||
if (delta.X != 0 || delta.Y != 0)
|
||||
{
|
||||
@ -90,17 +97,25 @@ namespace FPSExample
|
||||
|
||||
private void UpdateMovementInput()
|
||||
{
|
||||
if (Input.IsKeyPressed(KeyCode.W))
|
||||
m_MovementDirection.Y += 1.0f;
|
||||
else if (Input.IsKeyPressed(KeyCode.S))
|
||||
if (Input.IsKeyPressed(KeyCode.W)){
|
||||
m_MovementDirection.Y -= 1.0f;
|
||||
Debug.Log("KeyPressed: W");
|
||||
}
|
||||
else if (Input.IsKeyPressed(KeyCode.S)){
|
||||
m_MovementDirection.Y += 1.0f;
|
||||
Debug.Log("KeyPressed: S");
|
||||
}
|
||||
else
|
||||
m_MovementDirection.Y = 0.0f;
|
||||
|
||||
if(Input.IsKeyPressed(KeyCode.A))
|
||||
m_MovementDirection.X -= 1.0f;
|
||||
else if (Input.IsKeyPressed(KeyCode.D))
|
||||
if(Input.IsKeyPressed(KeyCode.A)){
|
||||
m_MovementDirection.X += 1.0f;
|
||||
Debug.Log("KeyPressed: A");
|
||||
}
|
||||
else if (Input.IsKeyPressed(KeyCode.D)){
|
||||
m_MovementDirection.X -= 1.0f;
|
||||
Debug.Log("KeyPressed: D");
|
||||
}
|
||||
else
|
||||
m_MovementDirection.X = 0.0f;
|
||||
|
||||
@ -136,26 +151,64 @@ namespace FPSExample
|
||||
|
||||
}
|
||||
|
||||
void OnPhysicsUpdate(float fixedTimeStep)
|
||||
{
|
||||
UpdateMovement();
|
||||
}
|
||||
|
||||
private void UpdateMovement()
|
||||
{
|
||||
m_RigidBody.Rotate(new Vec3(0.0f, m_CurrentYMovement, 0.0f));
|
||||
|
||||
Vec3 movement = m_CameraTransform.Transform.Right * m_MovementDirection.X + m_CameraTransform.Transform.Forward * m_MovementDirection.Y;
|
||||
// 2. 计算期望的移动方向(世界坐标系)
|
||||
Vec3 desiredDirection = Vec3.Zero;
|
||||
|
||||
if (m_MovementDirection.LengthSquared() != 0.0f)
|
||||
{
|
||||
Vec3 right = m_CameraTransform.Transform.Right;
|
||||
Vec3 forward = m_CameraTransform.Transform.Forward;
|
||||
right.Y = 0; forward.Y = 0;
|
||||
right.Normalize(); forward.Normalize();
|
||||
|
||||
desiredDirection = right * m_MovementDirection.X + forward * m_MovementDirection.Y;
|
||||
desiredDirection.Normalize();
|
||||
|
||||
/*
|
||||
Vec3 movement = right * m_MovementDirection.X + forward * m_MovementDirection.Y;
|
||||
movement.Normalize();
|
||||
Vec3 velocity = movement * m_CurrentSpeed;
|
||||
velocity.Y = m_RigidBody.GetLinearVelocity().Y;
|
||||
m_RigidBody.SetLinearVelocity(velocity);
|
||||
*/
|
||||
}
|
||||
Vec3 targetAngularVelocity = Vec3.Zero;
|
||||
if (desiredDirection.LengthSquared() > 0.01f)
|
||||
{
|
||||
Vec3 up = Vec3.Up;
|
||||
Vec3 rotationAxis = Vec3.Cross(desiredDirection, up).Normalized();
|
||||
float angularSpeed = 2 * (m_CurrentSpeed / m_Radius);
|
||||
targetAngularVelocity = rotationAxis * angularSpeed;
|
||||
|
||||
Vec3 currentAngular = m_RigidBody.GetAngularVelocity();
|
||||
Vec3 angularDiff = targetAngularVelocity - currentAngular;
|
||||
|
||||
float inertia = 0.4f * m_RigidBody.Mass * m_Radius * m_Radius;
|
||||
Vec3 torque = angularDiff * inertia * TorqueStrength;
|
||||
m_RigidBody.AddTorque(torque, RigidBodyComponent.ForceMode.Force);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// 3. 获取当前角速度,计算需要调整的差值
|
||||
Vec3 currentAngular = m_RigidBody.GetAngularVelocity();
|
||||
Vec3 angularDiff = targetAngularVelocity - currentAngular;
|
||||
|
||||
// 4. 施加扭矩:扭矩 = 转动惯量 * 角加速度
|
||||
// 球体的转动惯量 I = (2/5) * m * r^2 (实心球)
|
||||
float inertia = TorqueStrength * m_RigidBody.Mass * m_Radius * m_Radius;
|
||||
// 设定一个系数控制响应速度(可调)
|
||||
float torqueStrength = 5.0f;
|
||||
Vec3 torque = angularDiff * inertia * torqueStrength;
|
||||
m_RigidBody.AddTorque(-torque, RigidBodyComponent.ForceMode.Force);
|
||||
*/
|
||||
|
||||
if (m_ShouldJump && m_Colliding)
|
||||
{
|
||||
Debug.Log("Jump");
|
||||
m_RigidBody.AddForce(Vec3.Up * JumpForce, RigidBodyComponent.ForceMode.Impulse);
|
||||
m_ShouldJump = false;
|
||||
}
|
||||
@ -163,7 +216,7 @@ namespace FPSExample
|
||||
|
||||
private void UpdateCameraTransform(){
|
||||
Vec3 position = m_Transform.Translation + m_CameraTransform.Transform.Forward * CameraForwardOffset;
|
||||
position.Y += m_Transform.Translation.Y + CameraYOffset;
|
||||
position.Y += CameraYOffset;
|
||||
m_CameraTransform.Translation = position;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user