fixed track and controller working
This commit is contained in:
1 parent
50bc30f292
commit
7561677631
13 files changed
+15341
-7473
No files matched your search
+72
-62
@@ -1,9 +1,15 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class ShipControls : MonoBehaviour {
|
||||
public class ShipControls : MonoBehaviour, ShipInput.IMovementActions {
|
||||
public Camera aim;
|
||||
public Timing timing;
|
||||
|
||||
ShipInput shipInput;
|
||||
InputAction lookAction;
|
||||
InputAction accelerateAction;
|
||||
InputAction decelerateAction;
|
||||
|
||||
Rigidbody rigidBody;
|
||||
|
||||
Vector3 initialPosition;
|
||||
@@ -19,12 +25,9 @@ public class ShipControls : MonoBehaviour {
|
||||
ForceMode forceMode;
|
||||
|
||||
// Camera Controls
|
||||
float mouseSenseY = 1.0f;
|
||||
float mouseSenseX = 1.0f;
|
||||
float gamepadSenseY = 2.0f;
|
||||
float gamepadSenseX = 2.0f;
|
||||
float camPitch;
|
||||
float camYaw;
|
||||
float clampAngle = 90.0f;
|
||||
|
||||
void Start() {
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
@@ -48,23 +51,31 @@ public class ShipControls : MonoBehaviour {
|
||||
camPitch = initialAim.eulerAngles.x;
|
||||
camYaw = initialAim.eulerAngles.y;
|
||||
forceMode = ForceMode.Acceleration;
|
||||
|
||||
if (shipInput == null) {
|
||||
shipInput = new ShipInput();
|
||||
shipInput.movement.SetCallbacks(this);
|
||||
}
|
||||
shipInput.movement.Enable();
|
||||
|
||||
lookAction = shipInput.FindAction("look");
|
||||
accelerateAction = shipInput.FindAction("accelerate");
|
||||
decelerateAction = shipInput.FindAction("decelerate");
|
||||
}
|
||||
|
||||
void Update() {
|
||||
// update the movement vectors every frame
|
||||
// to minimise input lag we poll for the thrust vector every frame and set it
|
||||
thrust = getThrust();
|
||||
strafe = getStrafe();
|
||||
|
||||
// do everything else input related on every frame too
|
||||
processInput();
|
||||
// same with the camera, we always look around
|
||||
UpdateCamera();
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
|
||||
// when you hit the trigger...
|
||||
// GO
|
||||
// when you hit the trigger... GO
|
||||
// needs to be done here in fixedUpdate because otherwise the input is checked on every frame
|
||||
// so they will have already hit the button before this tick
|
||||
// so they will have already hit the button before this tick and they actually start moving
|
||||
// aka ripoff
|
||||
bool shouldStartTimer = !timing.timerEnabled && (thrust != Vector3.zero || strafe != Vector3.zero);
|
||||
if (shouldStartTimer)
|
||||
timing.StartTimer();
|
||||
@@ -82,51 +93,56 @@ public class ShipControls : MonoBehaviour {
|
||||
rigidBody.rotation = Quaternion.RotateTowards(transform.rotation, aim.transform.rotation, step);
|
||||
}
|
||||
|
||||
Vector3 getThrust() {
|
||||
// thrust in the direction of the camera
|
||||
if (Input.GetMouseButton(0))
|
||||
// transform from the camera's forward vector into world space
|
||||
return aim.transform.TransformDirection(Vector3.forward) * thrustMag;
|
||||
|
||||
// brake just decelerates the ship against the direction it is moving
|
||||
if (Input.GetMouseButton(1)) {
|
||||
void UpdateCamera() {
|
||||
// there's some hokey scaling that went on with the old input
|
||||
// we were originally using
|
||||
|
||||
// in the action mapping the mouse is scaled by 0.05
|
||||
// delta *= 0.5f; // Account for scaling applied directly in Windows code by old input system.
|
||||
// delta *= 0.1f; // Account for sensitivity setting on old Mouse X and Y axes.
|
||||
// def needs to be configurable
|
||||
|
||||
Vector2 lookDelta = lookAction.ReadValue<Vector2>();
|
||||
camYaw += lookDelta.x;
|
||||
camPitch -= lookDelta.y;
|
||||
camPitch = Mathf.Clamp(camPitch, -clampAngle, clampAngle);
|
||||
aim.transform.eulerAngles = new Vector3(camPitch, camYaw, 0.0f);
|
||||
}
|
||||
|
||||
public void OnStrafe(InputAction.CallbackContext ctx) {
|
||||
Vector2 str = ctx.ReadValue<Vector2>();
|
||||
strafe = new Vector3(str.x, str.y, 0f) * strafeMag;
|
||||
}
|
||||
|
||||
// if you are decelerating you can't accelerate as well
|
||||
// otherwise you can flip the ship around and
|
||||
// decelerate + accelerate against the velocity vector
|
||||
// aka cheating?
|
||||
Vector3 getThrust() {
|
||||
float accelerateInput = accelerateAction.ReadValue<float>();
|
||||
float decelerateInput = decelerateAction.ReadValue<float>();
|
||||
|
||||
// decelerates the ship against the direction it is moving
|
||||
if (decelerateInput > 0f) {
|
||||
// the velocity is already in world space so we just invert it
|
||||
return rigidBody.velocity.normalized * -1f * thrustMag;
|
||||
return rigidBody.velocity.normalized * -1f * thrustMag * decelerateInput;
|
||||
}
|
||||
|
||||
// thrust in the direction of the camera
|
||||
if (accelerateInput > 0f)
|
||||
// transform from the camera's forward vector into world space
|
||||
return aim.transform.TransformDirection(Vector3.forward) * thrustMag * accelerateInput;
|
||||
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
// 2D strafing
|
||||
// we combine all the vectors so you can move in 2 dimensions at once
|
||||
// thrust is added as a relative force so we don't need to convert to worldspace
|
||||
Vector3 getStrafe() {
|
||||
Vector3 strafe = new Vector3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
if (Input.GetKey(KeyCode.W))
|
||||
strafe += strafeMag * Vector3.up;
|
||||
if (Input.GetKey(KeyCode.S))
|
||||
strafe += strafeMag * Vector3.down;
|
||||
|
||||
if (Input.GetKey(KeyCode.A))
|
||||
strafe += strafeMag * Vector3.left;
|
||||
|
||||
if (Input.GetKey(KeyCode.D))
|
||||
strafe += strafeMag * Vector3.right;
|
||||
|
||||
return strafe;
|
||||
public void OnQuit(InputAction.CallbackContext ctx) {
|
||||
Application.Quit();
|
||||
}
|
||||
|
||||
void processInput() {
|
||||
updateCameraMouse();
|
||||
updateCameraGamepad();
|
||||
|
||||
// don't just spam respawn
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
Respawn();
|
||||
|
||||
if (Input.GetKey(KeyCode.F10))
|
||||
Application.Quit();
|
||||
public void OnRespawn(InputAction.CallbackContext ctx) {
|
||||
Respawn();
|
||||
}
|
||||
|
||||
public void Respawn() {
|
||||
@@ -145,19 +161,6 @@ public class ShipControls : MonoBehaviour {
|
||||
|
||||
camPitch = initialAim.eulerAngles.x;
|
||||
camYaw = initialAim.eulerAngles.y;
|
||||
|
||||
}
|
||||
|
||||
void updateCameraMouse() {
|
||||
camPitch -= mouseSenseY * Input.GetAxis("Mouse Y");
|
||||
camYaw += mouseSenseX * Input.GetAxis("Mouse X");
|
||||
aim.transform.eulerAngles = new Vector3(camPitch, camYaw, 0.0f);
|
||||
}
|
||||
|
||||
void updateCameraGamepad() {
|
||||
camPitch -= mouseSenseY * Input.GetAxis("Vertical");
|
||||
camYaw += mouseSenseX * Input.GetAxis("Horizontal");
|
||||
// aim.transform.eulerAngles = new Vector3(camPitch, camYaw, 0.0f);
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected() {
|
||||
@@ -187,4 +190,11 @@ public class ShipControls : MonoBehaviour {
|
||||
public float Speed() {
|
||||
return rigidBody.velocity.magnitude;
|
||||
}
|
||||
|
||||
// We don't use these because we poll instead
|
||||
// the callbacks are only invoked when the value changes
|
||||
// aka if you hold accelerate it doesn't do what you expect
|
||||
public void OnAccelerate(InputAction.CallbackContext ctx) { }
|
||||
public void OnDecelerate(InputAction.CallbackContext ctx) { }
|
||||
public void OnLook(InputAction.CallbackContext ctx) { }
|
||||
}
|
||||
Reference in new issue
Block a user