Created
September 4, 2013 04:12
-
-
Save Naddiseo/6432678 to your computer and use it in GitHub Desktop.
Unity PC/Ouya FPS manager
This file contains 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
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
class Player : MonoBehaviour { | |
public PlayerSettings settings = new PlayerSettings(); | |
public TextMesh tooltip = null; | |
PlayerInputManager input = null; | |
#region Player State | |
Vector3 last_position = Vector3.zero; | |
bool grounded = false; | |
bool running = false; | |
float headbob_stepcounter = 0; | |
float current_aim_ratio = 1.0f; | |
public Vector2 horizontal_movement; | |
Vector3 walk_deacceleration_velocity = Vector3.zero; | |
float x_rotation = 0F; | |
float y_rotation = 0F; | |
float current_x_rotation = 0F; | |
float current_y_rotation = 0F; | |
float x_rotation_velocity = 0F; | |
float y_rotation_velocity = 0F; | |
#endregion | |
public GameObject camera_object = null; | |
void Start() { | |
input = new PlayerInputManager(settings); | |
heartbeatSource = soundObject.GetComponent<AudioSource>(); | |
flashlight = GetComponent<Flashlight>(); | |
OuyaInput.SetContinuousScanning(true); | |
} | |
void Awake() { | |
last_position = transform.position; | |
} | |
void OnCollisionStay(Collision collision) { | |
foreach (var contact in collision.contacts) { | |
if (Vector3.Angle(contact.normal, Vector3.up) < settings.movement_settings.max_slope) { | |
grounded = true; | |
} | |
} | |
} | |
void OnCollisionExit() { | |
grounded = false; | |
} | |
void Update() { | |
OuyaInput.UpdateControllers(); | |
UpdateCamera(); | |
isRunning(); | |
} | |
void LateUpdate() { | |
UpdateHorizontalMovement(); | |
DoHeadBob(); | |
ApplyForces(); | |
} | |
void isRunning() { | |
// Can only change run speed if grounded | |
if (grounded) { | |
if (settings.movement_settings.run_toggle) { | |
if (input.GetButton(settings.control_settings.run)) { | |
running = !running; | |
} | |
} | |
else { | |
running = input.GetButton(settings.control_settings.run); | |
} | |
} | |
} | |
void ApplyForces() { | |
transform.rotation = Quaternion.Euler(0, current_y_rotation, 0); | |
float acceleration = settings.movement_settings.walk_acceleration; | |
if (!grounded) { | |
acceleration *= settings.movement_settings.walk_acceleration_air_ratio; | |
} | |
rigidbody.AddRelativeForce( | |
input.GetAxis(settings.control_settings.horizonal_axis) * acceleration * Time.deltaTime, | |
0, | |
input.GetAxis(settings.control_settings.vertical_axis) * acceleration * Time.deltaTime, | |
ForceMode.VelocityChange | |
); | |
if (input.GetButton(settings.control_settings.jump) && grounded) { | |
rigidbody.AddRelativeForce(0, settings.movement_settings.jump_velocity, 0); | |
} | |
} | |
void clamp_horizontal_speed(float max) { | |
if (horizontal_movement.magnitude > max) { | |
horizontal_movement.Normalize(); | |
horizontal_movement *= max; | |
rigidbody.velocity = new Vector3( | |
horizontal_movement.x, rigidbody.velocity.y, horizontal_movement.y | |
); | |
} | |
} | |
void UpdateHorizontalMovement() { | |
horizontal_movement = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z); | |
if (running) { | |
clamp_horizontal_speed(settings.movement_settings.max_run_speed); | |
} | |
else { | |
clamp_horizontal_speed(settings.movement_settings.max_walk_speed); | |
} | |
if (grounded) { | |
horizontal_movement.x = Mathf.SmoothDamp( | |
rigidbody.velocity.x, 0, ref walk_deacceleration_velocity.x,settings.movement_settings.slowdown_time | |
); | |
horizontal_movement.y = Mathf.SmoothDamp( | |
rigidbody.velocity.z, 0, ref walk_deacceleration_velocity.z, settings.movement_settings.slowdown_time | |
); | |
rigidbody.velocity = new Vector3(horizontal_movement.x, rigidbody.velocity.y, horizontal_movement.y); | |
} | |
} | |
void UpdateCamera() { | |
y_rotation += settings.camera_setting.x_sensitivity * input.GetAxis(settings.control_settings.look_horizontal_axis); | |
x_rotation -= settings.camera_setting.y_sensitivity * input.GetAxis(settings.control_settings.look_vertical_axis); | |
; | |
x_rotation = Mathf.Clamp(x_rotation,settings.camera_setting.min_x_rotation, settings.camera_setting.max_x_rotation); | |
current_x_rotation = Mathf.SmoothDamp(current_x_rotation, x_rotation, ref x_rotation_velocity, settings.camera_setting.look_smooth_damp); | |
current_y_rotation = Mathf.SmoothDamp(current_y_rotation, y_rotation, ref y_rotation_velocity, settings.camera_setting.look_smooth_damp); | |
camera_object.transform.rotation = Quaternion.Euler(current_x_rotation, current_y_rotation, 0); | |
} | |
void DoHeadBob() { | |
if (grounded) { | |
headbob_stepcounter += Vector3.Distance(last_position, transform.position) * settings.movement_settings.headbob_speed; | |
} | |
camera_object.transform.localPosition = new Vector3( | |
(Mathf.Sin(headbob_stepcounter) * settings.movement_settings.headbob_amount.x * current_aim_ratio), | |
( | |
(Mathf.Cos(headbob_stepcounter * 2) * settings.movement_settings.headbob_amount.y * -1 * current_aim_ratio) + | |
(transform.localScale.y * settings.movement_settings.eye_height_ratio) - | |
(transform.localScale.y / 2) | |
), | |
0 | |
); | |
last_position = transform.position; | |
} | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
class PlayerInputManager { | |
PlayerSettings settings; | |
public PlayerInputManager(PlayerSettings settings) { | |
this.settings = settings; | |
} | |
#if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_STANDALONE | |
public float GetAxis(OuyaAxis axis) { | |
return OuyaInput.GetAxis(axis, settings.player); | |
} | |
public bool GetButton(OuyaButton btn) { | |
return OuyaInput.GetButton(btn, settings.player); | |
} | |
#else | |
public float GetAxis(string name) { | |
return Input.GetAxis(name); | |
} | |
public bool GetButton(string name) { | |
return Input.GetButton(name); | |
} | |
#endif | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[System.Serializable] | |
class PlayerSettings { | |
[System.Serializable] | |
public class CameraSettings { | |
#if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_STANDALONE | |
public float x_sensitivity = 6F; | |
public float y_sensitivity = 6F; | |
public float look_smooth_damp = 0.3F; | |
public float default_camera_angle = 60F; | |
#else | |
public float x_sensitivity = 15F; | |
public float y_sensitivity = 15F; | |
public float look_smooth_damp = 0.3F; | |
public float default_camera_angle = 60F; | |
#endif | |
public float min_x_rotation = -90; | |
public float max_x_rotation = 90; | |
} | |
[System.Serializable] | |
public class MovementSettings { | |
public float walk_acceleration = 50f; // 50N = 1/ms | |
public float max_walk_speed = 1.4F; // m/s | |
public float max_run_speed = 2.2F; // m/s | |
public float max_slope = 60F; | |
public float jump_velocity = 150F; | |
public float walk_acceleration_air_ratio = 0.1F; | |
public float slowdown_time = 0.2F; | |
// Headbob | |
public float headbob_speed = 1; | |
public Vector2 headbob_amount = new Vector2(0.1f, 0.1f); | |
public float eye_height_ratio = 0.95f; | |
#if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_STANDALONE | |
public bool run_toggle = true; | |
#else | |
public bool run_toggle = false; | |
#endif | |
} | |
#if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_STANDALONE | |
public class ControlSettings { | |
[NonSerialized] | |
public OuyaAxis look_horizontal_axis = OuyaAxis.RX; | |
[NonSerialized] | |
public OuyaAxis look_vertical_axis = OuyaAxis.RY; | |
[NonSerialized] | |
public OuyaAxis horizonal_axis = OuyaAxis.LX; | |
[NonSerialized] | |
public OuyaAxis vertical_axis = OuyaAxis.LY; | |
[NonSerialized] | |
public OuyaButton jump = OuyaButton.O; | |
[NonSerialized] | |
public OuyaButton run = OuyaButton.L3; | |
[NonSerialized] | |
public OuyaButton flashlight = OuyaButton.DU; | |
} | |
#else | |
# if UNITY_EDITOR || UNITY_STANDALONE | |
[System.Serializable] | |
public class ControlSettings { | |
public string look_horizontal_axis = "Mouse X"; | |
public string look_vertical_axis = "Mouse Y"; | |
public string horizonal_axis = "Horizontal"; | |
public string vertical_axis = "Vertical"; | |
public string jump = "Jump"; | |
public string run = "Run"; | |
public string flashlight = "Flashlight"; | |
} | |
# endif | |
#endif | |
public CameraSettings camera_setting = new CameraSettings(); | |
public MovementSettings movement_settings = new MovementSettings(); | |
public ControlSettings control_settings = new ControlSettings(); | |
public OuyaPlayer player = OuyaPlayer.P01; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment