浏览代码

Simplification in First Person Controller

/main
Thomas ICHÉ 6 年前
当前提交
8b51a6ac
共有 3 个文件被更改,包括 46 次插入53 次删除
  1. 16
      Runtime/Controllers/FirstPersonController.cs
  2. 61
      Runtime/Controllers/KeyboardGamepadPlayerInput.cs
  3. 22
      Runtime/Controllers/PlayerInput.cs

16
Runtime/Controllers/FirstPersonController.cs


[Header("Metrics")]
[Range(1.0f, 2.5f)]
public float PlayerHeight;
public float PlayerHeight = 1.82f;
public float PlayerRadius;
public float PlayerRadius = 0.5f;
public float MoveSpeed;
public float TerminalVelocity = 54.16f;
public float TurnSpeed = 30;
public float MoveSpeed = 3.0f;
public float MaximumFallVelocity = 12f;
public float TurnSpeed = 180;
public bool EnableJump = false;
public bool EnableJump = true;
public float PitchSpeed = 30;
public float PitchSpeed = 180;
public float MaxPitch = 80;
private CharacterController m_Controller;

Vector3 move = (transform.forward * m_Input.Movement.y + transform.right * m_Input.Movement.x) * MoveSpeed;
m_Fall += PlayerWeight * 9.80665f * Time.deltaTime;
m_Fall = Mathf.Min(m_Fall, TerminalVelocity);
m_Fall = Mathf.Min(m_Fall, MaximumFallVelocity);
move += m_Fall * (-transform.up);
if (m_Grounded)

61
Runtime/Controllers/KeyboardGamepadPlayerInput.cs


{
public class KeyboardGamepadPlayerInput : PlayerInput
{
public bool useKeyboardAndMouse = true;
public bool useGamepad = true;
[Header("Behaviour")]
public float LookExponent = 2.0f;
[Range(0.0f, 0.7f)]

[Header("Buttons")]
public string JumpButton = "Jump";
public string PauseButton = "Pause";
public string ActionButton = "Action";
public string ShootButton = "Shoot";
public string UpButton = "Up";
public string DownButton = "Down";
public string LeftButton = "Left";
public string RightButton = "Right";
public override ButtonState Pause => m_Pause;
public override ButtonState Shoot => m_Shoot;
public override ButtonState Action => m_Action;
ButtonState m_Pause;
ButtonState m_Shoot;
ButtonState m_Action;
m_Movement = new Vector2(Input.GetAxisRaw(MovementHorizontalAxis), Input.GetAxisRaw(MovementVerticalAxis));
if(useGamepad || useKeyboardAndMouse)
{
m_Movement = new Vector2(Input.GetAxisRaw(MovementHorizontalAxis), Input.GetAxisRaw(MovementVerticalAxis));
if (m_Movement.magnitude < MovementDeadZone)
m_Movement = Vector2.zero;
}
Vector2 l = new Vector2(Input.GetAxisRaw(LookHorizontalAxis), Input.GetAxisRaw(LookVerticalAxis));
Vector2 ln = l.normalized;
float lm = Mathf.Clamp01(l.magnitude);
m_Look = ln * Mathf.Pow(Mathf.Clamp01(lm - LookDeadZone) / (1.0f - LookDeadZone), LookExponent);
m_Look = Vector2.zero;
if(useGamepad)
{
Vector2 l = new Vector2(Input.GetAxisRaw(LookHorizontalAxis), Input.GetAxisRaw(LookVerticalAxis));
Vector2 ln = l.normalized;
float lm = Mathf.Clamp01(l.magnitude);
m_Look += ln * Mathf.Pow(Mathf.Clamp01(lm - LookDeadZone) / (1.0f - LookDeadZone), LookExponent);
}
if (m_Movement.magnitude < MovementDeadZone) m_Movement = Vector2.zero;
m_Look += new Vector2(Input.GetAxisRaw(MouseHorizontalAxis), Input.GetAxisRaw(MouseVerticalAxis));
if(useKeyboardAndMouse)
m_Look += new Vector2(Input.GetAxisRaw(MouseHorizontalAxis), Input.GetAxisRaw(MouseVerticalAxis));
m_Pause = GetButtonState(PauseButton);
m_Action = GetButtonState(ActionButton);
m_Shoot = GetButtonState(ShootButton);
}
ButtonState GetButtonState(string Button)
{
if (Input.GetButton(Button))
{
if (Input.GetButtonDown(Button))
return ButtonState.JustPressed;
else
return ButtonState.Pressed;
}
else
{
if (Input.GetButtonUp(Button))
return ButtonState.JustReleased;
else
return ButtonState.Released;
}
}
}
}

22
Runtime/Controllers/PlayerInput.cs


public abstract Vector2 Look { get; }
public abstract Vector2 Movement { get; }
public abstract ButtonState Pause { get; }
public abstract ButtonState Shoot { get; }
public abstract ButtonState Action { get; }
protected static ButtonState GetButtonState(string Button)
{
if (Input.GetButton(Button))
{
if (Input.GetButtonDown(Button))
return ButtonState.JustPressed;
else
return ButtonState.Pressed;
}
else
{
if (Input.GetButtonUp(Button))
return ButtonState.JustReleased;
else
return ButtonState.Released;
}
}
}
}
正在加载...
取消
保存