Here you can see few things.
1 . I Forgot to disable mouse pointer in ShareX
2. My values is not very good.
3. Roll based on input does not work well with keyboard. Value can only be 1 or 0.
4. Unity new InputSystem in action.
Reimplementation of lecture script in new system.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[SerializeField] private InputAction fire = new InputAction("Fire", InputActionType.Button, null, null, null, "ButtonControl");
[SerializeField] private InputAction move = new InputAction("Move", InputActionType.Value, null, null, null, "Vector2Control");
[SerializeField] private float controlSpeed = 20f;
[SerializeField] private float xRange = 20f;
[SerializeField] private float yRange = 13f;
[SerializeField] private float positionPitchFactor = -2.5f;
[SerializeField] private float positionYawFactor = -0.5f;
[SerializeField] private float controlPitchFactor = -1.5f;
[SerializeField] private float controlRollFactor = -3f;
private Vector2 movementDirection;
private bool isFiring;
private bool isMoving;
private void OnEnable()
{
fire.Enable();
move.Enable();
}
private void OnDisable()
{
fire.Disable();
move.Disable();
}
private void Awake()
{
move.AddBinding("<Gamepad>/leftStick");
move.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
fire.AddBinding("<Mouse>/leftButton");
fire.AddBinding("<Gamepad>/rightTrigger");
fire.performed += ctx => OnFire();
fire.canceled += crx => OnFire();
move.started += ctx => OnMoveStarted();
move.performed += ctx => OnMove(ctx);
move.canceled += ctx => OnMoveCancel();
}
void Update()
{
ProcessInputState();
}
private void ProcessInputState()
{
if (isMoving)
{
Vector2 movementVector = movementDirection * (controlSpeed * Time.deltaTime);
float NewX = Mathf.Clamp(transform.localPosition.x + movementVector.x, -xRange, xRange);
float NewY = Mathf.Clamp(transform.localPosition.y + movementVector.y, -yRange, yRange);
transform.localPosition = new Vector3(NewX, NewY, transform.localPosition.z);
float pitch = transform.localPosition.y * positionPitchFactor + movementDirection.y * controlPitchFactor;
float yaw = transform.localPosition.x * positionYawFactor;
float roll = movementDirection.x * controlRollFactor;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
}
void OnFire()
{
isFiring = !isFiring;
}
void OnMoveStarted()
{
isMoving = true;
}
void OnMove(InputAction.CallbackContext ctx)
{
movementDirection = ctx.ReadValue<Vector2>();
}
void OnMoveCancel()
{
movementDirection = Vector2.zero;
isMoving = false;
}
}
New system premade components leaks memory. Wrapper generated from asset allows only one function per action and does not regenerated on asset save.