Created
October 1, 2018 14:13
-
-
Save LeviVisser/814143f07e1c5dc03f7f12402cc934e7 to your computer and use it in GitHub Desktop.
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 UnityEngine; | |
public class ShipMovementComponent : MonoBehaviour { | |
private Vector3 cameraStartingPos; | |
private Vector3 crosshairScreenPos; | |
private Vector3 crosshairViewPos; | |
private Vector3 crosshairWorldPos; | |
private float maxHorizontalCamChange; | |
private float MaxRotationRoll = 70; | |
private float maxVerticalCamChange; | |
private float pitchSpeed; | |
private float yawSpeed; | |
private float tilt = 4f; | |
public Transform MeshTransform; | |
public static ShipMovementComponent CreateComponent(GameObject targetObject, float maxHorizontalCamChange, float MaxRotationRoll, float maxVerticalCamChange, | |
float pitchSpeed, float yawSpeed, float tilt) | |
{ | |
ShipMovementComponent movementComponent = targetObject.AddComponent<ShipMovementComponent>(); | |
movementComponent.maxHorizontalCamChange = maxHorizontalCamChange; | |
movementComponent.MaxRotationRoll = MaxRotationRoll; | |
movementComponent.maxVerticalCamChange = maxVerticalCamChange; | |
movementComponent.pitchSpeed = pitchSpeed; | |
movementComponent.yawSpeed = yawSpeed; | |
movementComponent.tilt = tilt; | |
return movementComponent; | |
} | |
private void Awake() | |
{ | |
foreach(Transform child in transform) | |
{ | |
if(child.name == "ShipMesh") | |
{ | |
MeshTransform = child.gameObject.GetComponent<Transform>(); | |
} | |
} | |
} | |
private void Start() { | |
cameraStartingPos = Camera.main.transform.localPosition; | |
} | |
private void FixedUpdate() { | |
SetCrosshairPos(); | |
RotateShip(); | |
RollShip(); | |
} | |
/// <summary> | |
/// Updates all the different variations of the cross hair position | |
/// </summary> | |
private void SetCrosshairPos() { | |
crosshairScreenPos = GameObject.Find("Crosshair").transform.position; | |
crosshairViewPos = Camera.main.ScreenToViewportPoint(crosshairScreenPos); | |
crosshairWorldPos = PositionTransform.GetWorldPositionOnPlane(crosshairScreenPos, transform.position.z); | |
} | |
/// <summary> | |
/// A function that calculates how fast you need to turn around the y axis using the position of the cross hair | |
/// </summary> | |
/// <returns>A float between 0-1 with 0 being no turning at all</returns> | |
private float GetYawHardness() { | |
var xPos = Mathf.Clamp01(crosshairViewPos.x); | |
if (GoLeft()) { | |
return -((xPos - 0.5f) * -1) / 0.5f; | |
} | |
return (xPos - 0.5f) / 0.5f; | |
} | |
/// <summary> | |
/// A function that calculates how fast you need to turn around the x axis using the position of the cross hair | |
/// </summary> | |
/// <returns>A float between 0-1 with 0 being no turning at all</returns> | |
private float GetPitchHardness() { | |
var yPos = Mathf.Clamp01(crosshairViewPos.y); | |
if (GoUp()) { | |
return -(yPos - 0.5f) / 0.5f; | |
} | |
return (yPos - 0.5f) * -1 / 0.5f; | |
} | |
/// <summary> | |
/// Checks whether the cross hair is left of the center of the ship | |
/// </summary> | |
/// <returns>True if the cross hair is left of the ship</returns> | |
private bool GoLeft() { | |
var xShipPos = Mathf.Clamp01(Camera.main.WorldToViewportPoint(transform.position).x); | |
var xPos = Mathf.Clamp01(crosshairViewPos.x); | |
var left = xPos < xShipPos; | |
return left; | |
} | |
/// <summary> | |
/// Checks whether the cross hair is above the center of the ship | |
/// </summary> | |
/// <returns>True if the cross hair is above the ship</returns> | |
private bool GoUp() { | |
var yShipPos = Mathf.Clamp01(Camera.main.WorldToViewportPoint(transform.position).y); | |
var yPos = Mathf.Clamp01(crosshairViewPos.y); | |
return yPos > yShipPos; | |
} | |
/// <summary> | |
/// Applies the rotation generated by the Pitch and Yaw hardness | |
/// </summary> | |
public void RotateShip() { | |
//var rotation = transform.localRotation * Quaternion.AngleAxis(GetPitchHardness() * PitchSpeed, Vector3.right) * | |
// Quaternion.AngleAxis(GetYawHardness() * YawSpeed, Vector3.up); | |
transform.Rotate(GetPitchHardness() * pitchSpeed, 0, 0); | |
transform.Rotate(0, GetYawHardness() * yawSpeed, 0); | |
//transform.localRotation = Quaternion.Slerp(transform.localRotation, rotation, Time.deltaTime); | |
Camera.main.transform.localPosition = cameraStartingPos + new Vector3(GetYawHardness() * maxHorizontalCamChange, | |
-GetPitchHardness() * maxVerticalCamChange); | |
} | |
/// <summary> | |
/// Applies a rotation around the local z-axis | |
/// </summary> | |
private void RollShip() { | |
var rollLeftRight = ((Input.GetKey(KeyBinds.RollLeft) ? 1 : 0) + (Input.GetKey(KeyBinds.RollRight) ? -1 : 0)) * | |
tilt; | |
transform.Rotate(0, 0, rollLeftRight); | |
MeshTransform.localEulerAngles = Vector3.forward * (-GetYawHardness() * MaxRotationRoll); | |
} | |
/// <summary> | |
/// Checks whether the AddThrust button has been pressed | |
/// </summary> | |
/// <returns>True if the button is down</returns> | |
public bool AddThrust() { | |
return Input.GetKey(KeyBinds.AddThrust); | |
} | |
/// <summary> | |
/// Checks whether the RemoveThrust button has been pressed | |
/// </summary> | |
/// <returns>True if the button is down</returns> | |
public bool RemoveThrust() { | |
return Input.GetKey(KeyBinds.RemoveThrust); | |
} | |
/// <summary> | |
/// Checks whether the Boost button has been pressed | |
/// </summary> | |
/// <returns>True if the button is down</returns> | |
public bool ApplyBoost() { | |
return Input.GetKey(KeyBinds.Boost); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment