Created
October 27, 2021 13:27
-
-
Save Matthew-J-Spencer/32b69cfc71446a9af0d697be2cee585d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PlayerController : MonoBehaviour { | |
[SerializeField] private Rigidbody _rb; | |
[SerializeField] private float _speed = 5; | |
[SerializeField] private float _turnSpeed = 360; | |
private Vector3 _input; | |
private void Update() { | |
GatherInput(); | |
Look(); | |
} | |
private void FixedUpdate() { | |
Move(); | |
} | |
private void GatherInput() { | |
_input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); | |
} | |
private void Look() { | |
if (_input == Vector3.zero) return; | |
var rot = Quaternion.LookRotation(_input.ToIso(), Vector3.up); | |
transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, _turnSpeed * Time.deltaTime); | |
} | |
private void Move() { | |
_rb.MovePosition(transform.position + transform.forward * _input.normalized.magnitude * _speed * Time.deltaTime); | |
} | |
} | |
public static class Helpers | |
{ | |
private static Matrix4x4 _isoMatrix = Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0)); | |
public static Vector3 ToIso(this Vector3 input) => _isoMatrix.MultiplyPoint3x4(input); | |
} |
with this code, it's impossible to control the magnitude of the movement with a joystick, is it possible to fix this?
You can normalize the vector, so if its 0, the magnitude is 0, otherwise is 1. Done that, you can simply play with the speed variable to have an "on/off" movement or add some acceleration time based.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with this code, it's impossible to control the magnitude of the movement with a joystick, is it possible to fix this?