Skip to content

Instantly share code, notes, and snippets.

@verborghs
Created October 7, 2019 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verborghs/437819e815c2dd58cdbf221606d91469 to your computer and use it in GitHub Desktop.
Save verborghs/437819e815c2dd58cdbf221606d91469 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public class InputHandler : MonoBehaviour
{
private const string JoyStickVerticalTemplate = "{0}_Vertical";
private const string JoyStickHorizontalTemplate = "{0}_Horizontal";
[SerializeField]
private Camera _camera;
[SerializeField]
private PlayerBehaviour _player;
private void Start()
{
_camera = Camera.main;
}
private void Update()
{
if(Input.GetButtonDown("A"))
_player.Jump();
if(Input.GetButtonDown("B"))
_player.Interact();
var directionLeft = GetJoyStickDirection("LeftStick");
var worldDirectionLeft = ToWorldSpaceXYPlaneDirection(directionLeft);
_player.Move(directionLeft);
var directionRight = GetJoyStickDirection("RightStick");
var worldDirectionRight = ToWorldSpaceXYPlaneDirection(directionRight);
_player.Rotate(directionRight);
}
private Vector2 GetJoyStickDirection(String joyStickName)
{
float horizontal = UnityEngine.Input.GetAxis(String.Format(JoyStickHorizontalTemplate, joyStickName));
float vertical = UnityEngine.Input.GetAxis(String.Format(JoyStickVerticalTemplate, joyStickName));
Vector3 direction = new Vector2(horizontal, vertical);
return direction.sqrMagnitude > 2 ? direction.normalized : direction;
}
private Vector3 ToWorldSpaceXYPlaneDirection(Vector2 direction)
{
Vector3 cameraForward = Vector3.Scale(_camera.transform.forward, new Vector3(1, 0, 1)).normalized;
Vector3 cameraRight = Vector3.Scale(_camera.transform.right, new Vector3(1, 0, 1)).normalized;
return cameraRight * direction.x + cameraForward * direction.y;
}
}
using System;
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour
{
public float Speed;
public float JumpHeight;
private CharacterController _controller;
private Motor _motor;
private EnvironmentQuery _environmentQuery;
private Vector3 _movement;
private Vector3 _rotation;
private bool _interact;
private bool _jump;
void Start()
{
_controller = GetComponent<CharacterController>();
_motor = new Motor(_controller, JumpHeight, Speed);
_environmentQuery = new EnvironmentQuery(2,1);
}
void FixedUpdate()
{
_motor.Begin();
//Determine the player surface
var groundNormal = Vector3.up;
var groundPoint = Vector3.zero;
var characterCenter = transform.position + _controller.center;
groundPoint = _environmentQuery.FindGroundPoint(characterCenter);
groundNormal = _environmentQuery.FindGroundNormal(groundPoint, characterCenter);
if (IsZeroLengthVector(_rotation))
{
ApplyRotation(_rotation);
}
_motor.ApplyGravity();
if (_controller.isGrounded)
{
_motor.ApplyGround(groundNormal);
_motor.ApplyMovement(_movement, groundNormal);
if (_jump)
{
_motor.ApplyJump();
_jump = false;
}
else
{
if (_interact)
{
var interactive = _environmentQuery.FindInteractives();
if (interactive != null)
{
InteractWith(interactive);
}
_interact = false;
}
}
}
_motor.Commit();
}
public void Move(Vector3 worldDirection)
{
_movement = worldDirection;
}
public void Rotate(Vector3 worldDirection)
{
_rotation = worldDirection;
}
public void Interact()
{
_interact = true;
}
public void Jump()
{
_jump = true;
}
private void ApplyRotation(Vector3 worldDirection)
{
Quaternion worldOrientation = Quaternion.LookRotation(worldDirection, Vector3.up);
//rotate the player
transform.rotation = worldOrientation;
}
private bool IsZeroLengthVector(Vector3 worldDirectionRight)
{
return !Mathf.Approximately(worldDirectionRight.sqrMagnitude, 0.0f);
}
private void InteractWith(Collider interactive)
{
var physics = interactive.GetComponentInParent<Rigidbody>();
Vector3 direction = interactive.transform.position - transform.position;
direction.y = 0;
physics.AddForce(direction * 8.0f, ForceMode.Impulse);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment