added basic controls

Added a player with basic movement/rotation controls.
Uses default Input Actions, so most controllers should work.
This commit is contained in:
Banane_Rotative
2025-03-04 15:12:07 +01:00
parent 177bf17294
commit 2bc676efed
12 changed files with 2338 additions and 41 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f4c053d580b26d14ab03dc9a21f90ee3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,58 @@
using UnityEngine;
// Handles everything related to player movement
public class PlayerController : MonoBehaviour
{
private PlayerControlsMap playerControls;
private Rigidbody rb;
[SerializeField] private float speed = 15;
[SerializeField] private float sensitivity = 50;
[SerializeField] private float maxSpeed = 10;
// Awake is always called before Start. Often used to initialize variables
void Awake()
{
playerControls = new PlayerControlsMap();
rb = GetComponent<Rigidbody>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
// Often used to initialize variables which are dependent to other scripts/components
void Start()
{
}
void OnEnable()
{
playerControls.Enable();
}
void OnDisable()
{
playerControls.Disable();
}
// Update is called once per frame
void Update()
{
// Update player direction. Very basic.
Vector2 lookInput = playerControls.Player.Look.ReadValue<Vector2>();
lookInput *= sensitivity * Time.deltaTime;
Quaternion currentRotation = transform.rotation;
currentRotation *= Quaternion.AngleAxis(lookInput.x, Vector3.up);
transform.rotation = currentRotation;
// We ignore vertical rotation as it would have no impact on the game
// Update player acceleration, using a force
Vector2 mvtInput = playerControls.Player.Move.ReadValue<Vector2>();
mvtInput *= speed * Time.deltaTime;
rb.AddForce(currentRotation * new Vector3(mvtInput.x, 0, mvtInput.y), ForceMode.VelocityChange);
// Limit max speed. Warning: both maxSpeed and rigidbody linear damping affect max speed
if (rb.linearVelocity.magnitude > maxSpeed) {
rb.linearVelocity = rb.linearVelocity.normalized * maxSpeed;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 477e133ae7cec0f44b29e6fb665c5e00

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0540f779c45ae6e499f62ce2f8837a9b

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,14 @@
fileFormatVersion: 2
guid: 052faaac586de48259a63d0c4782560b
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3}
generateWrapperCode: 1
wrapperCodePath:
wrapperClassName:
wrapperCodeNamespace: