Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gamepad controller support #58

Open
wants to merge 6 commits into
base: Development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions Assets/Scripts/Game/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,23 @@ void InitRigidbody () {
}

void Update () {
HandleMovement ();
}
}

void FixedUpdate()
{
HandleMovement();
HandleCelestialBodies();

}

void HandleMovement () {
void HandleMovement () {
HandleEditorInput ();
if (Time.timeScale == 0) {
return;
}
// Look input
yaw += Input.GetAxisRaw ("Mouse X") * inputSettings.mouseSensitivity / 10 * mouseSensitivityMultiplier;
pitch -= Input.GetAxisRaw ("Mouse Y") * inputSettings.mouseSensitivity / 10 * mouseSensitivityMultiplier;
yaw += Input.GetAxisRaw ("Look X") * inputSettings.mouseSensitivity / 10 * mouseSensitivityMultiplier;
pitch -= Input.GetAxisRaw ("Look Y") * inputSettings.mouseSensitivity / 10 * mouseSensitivityMultiplier;
pitch = Mathf.Clamp (pitch, pitchMinMax.x, pitchMinMax.y);
float mouseSmoothTime = Mathf.Lerp (0.01f, maxMouseSmoothTime, inputSettings.mouseSmoothing);
smoothPitch = Mathf.SmoothDampAngle (smoothPitch, pitch, ref pitchSmoothV, mouseSmoothTime);
Expand All @@ -101,13 +107,13 @@ void HandleMovement () {
// Movement
bool isGrounded = IsGrounded ();
Vector3 input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
bool running = Input.GetKey (KeyCode.LeftShift);
bool running = Input.GetAxis("Throttle") < 0;
targetVelocity = transform.TransformDirection (input.normalized) * ((running) ? runSpeed : walkSpeed);
smoothVelocity = Vector3.SmoothDamp (smoothVelocity, targetVelocity, ref smoothVRef, (isGrounded) ? vSmoothTime : airSmoothTime);

//bool inWater = referenceBody
if (isGrounded) {
if (Input.GetKeyDown (KeyCode.Space)) {
if (Input.GetAxis("Throttle") > 0) {
rb.AddForce (transform.up * jumpForce, ForceMode.VelocityChange);
isGrounded = false;
} else {
Expand All @@ -116,12 +122,12 @@ void HandleMovement () {
}
} else {
// Press (and hold) spacebar while above ground to engage jetpack
if (Input.GetKeyDown (KeyCode.Space)) {
if (Input.GetAxis("Throttle") > 0) {
usingJetpack = true;
}
}

if (usingJetpack && Input.GetKey (KeyCode.Space) && jetpackFuelPercent > 0) {
if (usingJetpack && Input.GetAxis("Throttle") > 0 && jetpackFuelPercent > 0) {
lastJetpackUseTime = Time.time;
jetpackFuelPercent -= Time.deltaTime / jetpackDuration;
rb.AddForce (transform.up * jetpackForce, ForceMode.Acceleration);
Expand Down Expand Up @@ -164,8 +170,9 @@ bool IsGrounded () {
return grounded;
}

void FixedUpdate () {
CelestialBody[] bodies = NBodySimulation.Bodies;
void HandleCelestialBodies()
{
CelestialBody[] bodies = NBodySimulation.Bodies;
Vector3 gravityOfNearestBody = Vector3.zero;
float nearestSurfaceDst = float.MaxValue;

Expand Down Expand Up @@ -227,4 +234,4 @@ public Rigidbody Rigidbody {
}
}

}
}
26 changes: 8 additions & 18 deletions Assets/Scripts/Game/Controllers/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ void Update () {
}

void HandleMovement () {
Debug.Log(Input.GetAxis("Select"));
// Thruster input
int thrustInputX = GetInputAxis (leftKey, rightKey);
int thrustInputY = GetInputAxis (descendKey, ascendKey);
int thrustInputZ = GetInputAxis (backwardKey, forwardKey);
float thrustInputX = Input.GetAxis("Horizontal");
float thrustInputY = Input.GetAxis("Throttle");
float thrustInputZ = Input.GetAxis("Vertical");
thrusterInput = new Vector3 (thrustInputX, thrustInputY, thrustInputZ);

// Rotation input
float yawInput = Input.GetAxisRaw ("Mouse X") * rotSpeed * inputSettings.mouseSensitivity / 100f;
float pitchInput = Input.GetAxisRaw ("Mouse Y") * rotSpeed * inputSettings.mouseSensitivity / 100f;
float rollInput = GetInputAxis (rollCounterKey, rollClockwiseKey) * rollSpeed * Time.deltaTime;
float yawInput = Input.GetAxisRaw ("Look X") * rotSpeed * inputSettings.mouseSensitivity / 100f;
float pitchInput = Input.GetAxisRaw ("Look Y") * rotSpeed * inputSettings.mouseSensitivity / 100f;
float rollInput = Input.GetAxis("Roll") * rollSpeed * Time.deltaTime;

// Calculate rotation
if (numCollisionTouches == 0) {
Expand Down Expand Up @@ -105,17 +106,6 @@ void TeleportToBody (CelestialBody body) {
rb.MovePosition (body.transform.position + (transform.position - body.transform.position).normalized * body.radius * 2);
}

int GetInputAxis (KeyCode negativeAxis, KeyCode positiveAxis) {
int axis = 0;
if (Input.GetKey (positiveAxis)) {
axis++;
}
if (Input.GetKey (negativeAxis)) {
axis--;
}
return axis;
}

void HandleCheats () {
if (Universe.cheatsEnabled) {
if (Input.GetKeyDown (KeyCode.Return) && IsPiloted && Time.timeScale != 0) {
Expand Down Expand Up @@ -209,4 +199,4 @@ public Rigidbody Rigidbody {
}
}

}
}
15 changes: 12 additions & 3 deletions Assets/Scripts/Game/Interactions/Interactable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@

public class Interactable : MonoBehaviour {

public string interactMessage = "Press F to interact";
public string interactMessage = "Press F (Y) to interact";
protected bool playerInInteractionZone;
public UnityEngine.Events.UnityEvent interactEvent;

bool selecting = false;

protected virtual void Interact () {
if (interactEvent != null) {
interactEvent.Invoke ();
}
}

protected virtual void Update () {
if (playerInInteractionZone && Input.GetKeyDown (KeyCode.F)) {
if (playerInInteractionZone && Input.GetAxis("Toggle") > 0) {
if (selecting) {
return;
}
selecting = true;
GameUI.CancelInteractionDisplay ();
Interact ();
}
else {
selecting = false;
}
}

protected virtual void OnTriggerEnter (Collider c) {
Expand All @@ -42,4 +51,4 @@ public void ForcePlayerInInteractionZone () {
playerInInteractionZone = true;
}

}
}
13 changes: 10 additions & 3 deletions Assets/Scripts/Game/ShipHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ShipHUD : MonoBehaviour {
LockOnUI lockOnUI;
Ship ship;
CelestialBody aimedBody;
bool selecting = false;

void Start () {
// Need to draw UI AFTER floating origin updates, otherwise may flicker when origin changes
Expand Down Expand Up @@ -65,14 +66,20 @@ void UpdateUI () {

if (Time.timeScale != 0) {
aimedBody = FindAimedBody ();

if (Input.GetMouseButtonDown (0)) {
if (Input.GetAxis("Select") > 0 || Input.GetMouseButtonDown(0)) {
if (selecting) {
return;
}
selecting = true;
if (lockedBody == aimedBody) {
lockedBody = null;
} else {
lockedBody = aimedBody;
}
}
else {
selecting = false;
}
}

if (aimedBody && aimedBody != lockedBody) {
Expand Down Expand Up @@ -275,4 +282,4 @@ public void SetActive (bool active) {
head.gameObject.SetActive (active);
}
}
}
}
Loading