Re-think singleton player idea, add raycasting to stop from falling off ledges
This commit is contained in:
32
Assets/Scripts/Input/CharacterInputController.cs
Normal file
32
Assets/Scripts/Input/CharacterInputController.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Scampz.GameJam.Assets.Scripts
|
||||
{
|
||||
public class CharacterInputController : MonoBehaviour
|
||||
{
|
||||
private CharacterController _controller;
|
||||
private RayCaster _caster;
|
||||
public float Speed = 10f;
|
||||
public float RotateSpeed = 1.0f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_controller = GetComponent<CharacterController>();
|
||||
_caster = _controller.GetComponent<RayCaster>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Rotation
|
||||
transform.Rotate(0, Input.GetAxis(InputOptions.Horizontal) * RotateSpeed, 0);
|
||||
|
||||
// Move
|
||||
if (_caster.IsWithinBounds())
|
||||
{
|
||||
var forward = transform.TransformDirection(Vector3.forward);
|
||||
var curSpeed = Speed * Input.GetAxis(InputOptions.Vertical);
|
||||
_controller.SimpleMove(forward * curSpeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b39191ac21e3c3c46b862363cf286165
|
||||
guid: 05cf47d54a6d28d47b5d9b54e95d2751
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -1,15 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Scampz.GameJam.Assets.Scripts
|
||||
{
|
||||
public class InputManager : MonoBehaviour
|
||||
{
|
||||
public static InputManager Instance { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
Instance = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Assets/Scripts/Input/RayCaster.cs
Normal file
22
Assets/Scripts/Input/RayCaster.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Scampz.GameJam.Assets.Scripts
|
||||
{
|
||||
public class RayCaster : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private float rayCastDistance = 1f;
|
||||
|
||||
public bool IsWithinBounds()
|
||||
{
|
||||
var offsetAngle = Quaternion.AngleAxis(45, transform.right);
|
||||
return Physics.Raycast(transform.position, offsetAngle * transform.forward);
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
var offsetAngle = Quaternion.AngleAxis(45, transform.right);
|
||||
Debug.DrawRay(transform.position, offsetAngle * transform.forward * rayCastDistance);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Input/RayCaster.cs.meta
Normal file
11
Assets/Scripts/Input/RayCaster.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e12a799f5a50bc34a94898baf2917b7b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user