fixed track and controller working

This commit is contained in:
ntr committed 2023-07-11 00:42:43 +10:00
1 parent 43b21a1233
commit c250388bf1
13 files changed
+15341 -7473

No files matched your search

+72 -62
View File
@@ -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) { }
}
Regular → Executable
+501 -10
View File
@@ -1,13 +1,504 @@
using UnityEngine;
//------------------------------------------------------------------------------
// <auto-generated>
// This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator
// version 1.6.1
// from Assets/Resources/ShipInput.inputactions
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
public class ShipInput : MonoBehaviour {
public InputAction accelerate;
public InputAction decelerate;
public InputAction look;
public InputAction respawn;
public InputAction quit;
void Start() {
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
public partial class @ShipInput: IInputActionCollection2, IDisposable
{
public InputActionAsset asset { get; }
public @ShipInput()
{
asset = InputActionAsset.FromJson(@"{
""name"": ""ShipInput"",
""maps"": [
{
""name"": ""movement"",
""id"": ""f52df5ad-4b20-4a0e-a4c8-bbf76c26b2c5"",
""actions"": [
{
""name"": ""accelerate"",
""type"": ""Value"",
""id"": ""3d5d2223-bd99-4486-a95d-cefe220e0257"",
""expectedControlType"": ""Axis"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": true
},
{
""name"": ""decelerate"",
""type"": ""Value"",
""id"": ""a27ee77d-59bd-4935-9a7b-961c2da0faf7"",
""expectedControlType"": ""Axis"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": true
},
{
""name"": ""strafe"",
""type"": ""Value"",
""id"": ""d8d4baa2-2812-45af-99f6-eab49bef6b61"",
""expectedControlType"": ""Vector2"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": true
},
{
""name"": ""look"",
""type"": ""Value"",
""id"": ""f705d9c6-7be0-40e0-95ba-990e6892d4dc"",
""expectedControlType"": ""Vector2"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": true
},
{
""name"": ""respawn"",
""type"": ""Button"",
""id"": ""2ced6ae8-aba2-4cd7-882e-6e6473308812"",
""expectedControlType"": ""Button"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": false
},
{
""name"": ""quit"",
""type"": ""Button"",
""id"": ""cea65125-c42e-40e9-bc97-966522e16691"",
""expectedControlType"": ""Button"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": false
}
],
""bindings"": [
{
""name"": """",
""id"": ""5e57f603-6e0e-4288-a67e-57c8a8068092"",
""path"": ""<Mouse>/leftButton"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""accelerate"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""f1ab715c-3909-449b-a5b9-0abc49bf8008"",
""path"": ""<Gamepad>/rightTrigger"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""accelerate"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""43eea80c-a658-4eee-b6be-2f0124f2785d"",
""path"": ""<Mouse>/rightButton"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""decelerate"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""72aff4e4-d43a-4865-8ce5-42a26fb8270d"",
""path"": ""<Gamepad>/leftTrigger"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""decelerate"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""d8bfc5bc-3673-4825-874c-c42f24765ce6"",
""path"": ""<Keyboard>/space"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""respawn"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""7bde4731-2ad0-45f2-9fcf-b8a20a6ee7ac"",
""path"": ""<Gamepad>/buttonEast"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""respawn"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""6cb625e2-60f3-49d5-9d12-8265a4aab0a4"",
""path"": ""<Gamepad>/buttonSouth"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""respawn"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""b3126289-9c10-4247-9cee-bf97a20f429b"",
""path"": ""<Gamepad>/buttonWest"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""respawn"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""c87398b5-3170-4be8-80b3-b6304e61bba4"",
""path"": ""<Gamepad>/buttonNorth"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""respawn"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""ff02b06d-2805-46a1-9834-1d3be43bcea3"",
""path"": ""<Keyboard>/f10"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""quit"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""6eca2679-7a64-4e8e-ade3-9ad4d7eafa82"",
""path"": ""<Gamepad>/leftStick"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": ""WSAD"",
""id"": ""8a7788e1-720b-46ee-8253-ffba334daff6"",
""path"": ""2DVector"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": true,
""isPartOfComposite"": false
},
{
""name"": ""up"",
""id"": ""6c646c3c-8579-43a9-95c2-06a56a54d226"",
""path"": ""<Keyboard>/w"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""down"",
""id"": ""9e73f516-8345-4aea-9c07-04081574d1b9"",
""path"": ""<Keyboard>/s"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""left"",
""id"": ""923dbff2-8bd5-42e9-814d-63726354fca6"",
""path"": ""<Keyboard>/a"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""right"",
""id"": ""ae3937ef-d1dc-4556-a171-042fe9bd989f"",
""path"": ""<Keyboard>/d"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""Arrows"",
""id"": ""71f8d215-e03a-44a6-ac20-ee132c3f202f"",
""path"": ""2DVector"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": true,
""isPartOfComposite"": false
},
{
""name"": ""up"",
""id"": ""4f9ff5b2-2e55-4180-9546-6f3c3a334e13"",
""path"": ""<Keyboard>/upArrow"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""down"",
""id"": ""1f317310-50fe-4c74-8380-0f94b4699fce"",
""path"": ""<Keyboard>/downArrow"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""left"",
""id"": ""bb65e099-37da-4d59-b52d-39a117595f71"",
""path"": ""<Keyboard>/leftArrow"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""right"",
""id"": ""fedaa785-c164-459b-8743-09171e6c531d"",
""path"": ""<Keyboard>/rightArrow"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""strafe"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": """",
""id"": ""fcff1aee-823d-4930-b94b-265846f4dc30"",
""path"": ""<Mouse>/delta"",
""interactions"": """",
""processors"": ""ScaleVector2(x=0.05,y=0.05)"",
""groups"": """",
""action"": ""look"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""55976656-0c45-48bc-8c60-bdd71583f463"",
""path"": ""<Gamepad>/rightStick"",
""interactions"": """",
""processors"": ""ScaleVector2(x=0.75,y=0.75)"",
""groups"": """",
""action"": ""look"",
""isComposite"": false,
""isPartOfComposite"": false
}
]
}
],
""controlSchemes"": []
}");
// movement
m_movement = asset.FindActionMap("movement", throwIfNotFound: true);
m_movement_accelerate = m_movement.FindAction("accelerate", throwIfNotFound: true);
m_movement_decelerate = m_movement.FindAction("decelerate", throwIfNotFound: true);
m_movement_strafe = m_movement.FindAction("strafe", throwIfNotFound: true);
m_movement_look = m_movement.FindAction("look", throwIfNotFound: true);
m_movement_respawn = m_movement.FindAction("respawn", throwIfNotFound: true);
m_movement_quit = m_movement.FindAction("quit", throwIfNotFound: true);
}
}
public void Dispose()
{
UnityEngine.Object.Destroy(asset);
}
public InputBinding? bindingMask
{
get => asset.bindingMask;
set => asset.bindingMask = value;
}
public ReadOnlyArray<InputDevice>? devices
{
get => asset.devices;
set => asset.devices = value;
}
public ReadOnlyArray<InputControlScheme> controlSchemes => asset.controlSchemes;
public bool Contains(InputAction action)
{
return asset.Contains(action);
}
public IEnumerator<InputAction> GetEnumerator()
{
return asset.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Enable()
{
asset.Enable();
}
public void Disable()
{
asset.Disable();
}
public IEnumerable<InputBinding> bindings => asset.bindings;
public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = false)
{
return asset.FindAction(actionNameOrId, throwIfNotFound);
}
public int FindBinding(InputBinding bindingMask, out InputAction action)
{
return asset.FindBinding(bindingMask, out action);
}
// movement
private readonly InputActionMap m_movement;
private List<IMovementActions> m_MovementActionsCallbackInterfaces = new List<IMovementActions>();
private readonly InputAction m_movement_accelerate;
private readonly InputAction m_movement_decelerate;
private readonly InputAction m_movement_strafe;
private readonly InputAction m_movement_look;
private readonly InputAction m_movement_respawn;
private readonly InputAction m_movement_quit;
public struct MovementActions
{
private @ShipInput m_Wrapper;
public MovementActions(@ShipInput wrapper) { m_Wrapper = wrapper; }
public InputAction @accelerate => m_Wrapper.m_movement_accelerate;
public InputAction @decelerate => m_Wrapper.m_movement_decelerate;
public InputAction @strafe => m_Wrapper.m_movement_strafe;
public InputAction @look => m_Wrapper.m_movement_look;
public InputAction @respawn => m_Wrapper.m_movement_respawn;
public InputAction @quit => m_Wrapper.m_movement_quit;
public InputActionMap Get() { return m_Wrapper.m_movement; }
public void Enable() { Get().Enable(); }
public void Disable() { Get().Disable(); }
public bool enabled => Get().enabled;
public static implicit operator InputActionMap(MovementActions set) { return set.Get(); }
public void AddCallbacks(IMovementActions instance)
{
if (instance == null || m_Wrapper.m_MovementActionsCallbackInterfaces.Contains(instance)) return;
m_Wrapper.m_MovementActionsCallbackInterfaces.Add(instance);
@accelerate.started += instance.OnAccelerate;
@accelerate.performed += instance.OnAccelerate;
@accelerate.canceled += instance.OnAccelerate;
@decelerate.started += instance.OnDecelerate;
@decelerate.performed += instance.OnDecelerate;
@decelerate.canceled += instance.OnDecelerate;
@strafe.started += instance.OnStrafe;
@strafe.performed += instance.OnStrafe;
@strafe.canceled += instance.OnStrafe;
@look.started += instance.OnLook;
@look.performed += instance.OnLook;
@look.canceled += instance.OnLook;
@respawn.started += instance.OnRespawn;
@respawn.performed += instance.OnRespawn;
@respawn.canceled += instance.OnRespawn;
@quit.started += instance.OnQuit;
@quit.performed += instance.OnQuit;
@quit.canceled += instance.OnQuit;
}
private void UnregisterCallbacks(IMovementActions instance)
{
@accelerate.started -= instance.OnAccelerate;
@accelerate.performed -= instance.OnAccelerate;
@accelerate.canceled -= instance.OnAccelerate;
@decelerate.started -= instance.OnDecelerate;
@decelerate.performed -= instance.OnDecelerate;
@decelerate.canceled -= instance.OnDecelerate;
@strafe.started -= instance.OnStrafe;
@strafe.performed -= instance.OnStrafe;
@strafe.canceled -= instance.OnStrafe;
@look.started -= instance.OnLook;
@look.performed -= instance.OnLook;
@look.canceled -= instance.OnLook;
@respawn.started -= instance.OnRespawn;
@respawn.performed -= instance.OnRespawn;
@respawn.canceled -= instance.OnRespawn;
@quit.started -= instance.OnQuit;
@quit.performed -= instance.OnQuit;
@quit.canceled -= instance.OnQuit;
}
public void RemoveCallbacks(IMovementActions instance)
{
if (m_Wrapper.m_MovementActionsCallbackInterfaces.Remove(instance))
UnregisterCallbacks(instance);
}
public void SetCallbacks(IMovementActions instance)
{
foreach (var item in m_Wrapper.m_MovementActionsCallbackInterfaces)
UnregisterCallbacks(item);
m_Wrapper.m_MovementActionsCallbackInterfaces.Clear();
AddCallbacks(instance);
}
}
public MovementActions @movement => new MovementActions(this);
public interface IMovementActions
{
void OnAccelerate(InputAction.CallbackContext context);
void OnDecelerate(InputAction.CallbackContext context);
void OnStrafe(InputAction.CallbackContext context);
void OnLook(InputAction.CallbackContext context);
void OnRespawn(InputAction.CallbackContext context);
void OnQuit(InputAction.CallbackContext context);
}
}
-4
View File
@@ -27,8 +27,4 @@ public class TrackLimits : MonoBehaviour {
return;
}
}
void HitTrackLimits() {
}
}