169 lines
5.5 KiB
C#
Executable File
169 lines
5.5 KiB
C#
Executable File
using UnityEngine;
|
|
|
|
public class ShipControls : MonoBehaviour {
|
|
public Camera m_camera;
|
|
|
|
Rigidbody m_rigidBody;
|
|
|
|
Vector3 m_initialPosition;
|
|
Vector3 m_initialForce;
|
|
Quaternion m_initialCamera;
|
|
|
|
Vector3 m_thrust;
|
|
Vector3 m_strafe;
|
|
|
|
float m_strafeMag;
|
|
float m_thrustMag;
|
|
float m_stabliseMag;
|
|
ForceMode m_forceMode;
|
|
|
|
// Camera Controls
|
|
float m_mouseSenseY = 1.0f;
|
|
float m_mouseSenseX = 1.0f;
|
|
float m_camPitch;
|
|
float m_camYaw;
|
|
|
|
void Start() {
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
m_rigidBody = GetComponent<Rigidbody>();
|
|
|
|
// just stop the body from being affected by rotation for now
|
|
// until the 3p camera is in to explain why
|
|
m_rigidBody.freezeRotation = true;
|
|
|
|
m_initialPosition = m_rigidBody.transform.position;
|
|
m_initialCamera = m_camera.transform.rotation;
|
|
m_initialForce = Vector3.forward;
|
|
|
|
m_thrust = Vector3.zero;
|
|
m_strafe = Vector3.zero;
|
|
|
|
m_thrustMag = 50.0f;
|
|
m_strafeMag = 25.0f;
|
|
m_stabliseMag = 75.0f;
|
|
|
|
m_camPitch = m_initialCamera.eulerAngles.x;
|
|
m_camYaw = m_initialCamera.eulerAngles.y;
|
|
m_forceMode = ForceMode.Acceleration;
|
|
}
|
|
|
|
void Update() {
|
|
// update the movement vectors every frame
|
|
m_thrust = getThrust();
|
|
m_strafe = getStrafe();
|
|
|
|
// do everything else input related on every frame too
|
|
processInput();
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
// the ship being propelled forward
|
|
m_rigidBody.AddForce(m_thrust, m_forceMode);
|
|
|
|
// moving side to side
|
|
m_rigidBody.AddRelativeForce(m_strafe, m_forceMode);
|
|
|
|
// rotating towards the camera's orientation
|
|
// not being done by a force because of strange behaviour
|
|
// needs investigation
|
|
float step = m_stabliseMag * Time.fixedDeltaTime;
|
|
m_rigidBody.rotation = Quaternion.RotateTowards(transform.rotation, m_camera.transform.rotation, step);
|
|
}
|
|
|
|
Vector3 getThrust() {
|
|
// thrust in the direction of the camera
|
|
if (Input.GetMouseButton(0))
|
|
// transform from the camera's forward vector into world space
|
|
return m_camera.transform.TransformDirection(Vector3.forward) * m_thrustMag;
|
|
|
|
// brake just decelerates the ship against the direction it is moving
|
|
if (Input.GetMouseButton(1)) {
|
|
// the velocity is already in world space so we just invert it
|
|
return m_rigidBody.velocity.normalized * -1f * m_thrustMag;
|
|
}
|
|
|
|
return Vector3.zero;
|
|
}
|
|
|
|
// 2D strafing
|
|
// we combine all the vectors so you can move in 2 dimensions at once
|
|
// thrust is added as a relative force so we don't need to convert to worldspace
|
|
Vector3 getStrafe() {
|
|
Vector3 strafe = new Vector3(0.0f, 0.0f, 0.0f);
|
|
|
|
if (Input.GetKey(KeyCode.W))
|
|
strafe += m_strafeMag * Vector3.up;
|
|
if (Input.GetKey(KeyCode.S))
|
|
strafe += m_strafeMag * Vector3.down;
|
|
|
|
if (Input.GetKey(KeyCode.A))
|
|
strafe += m_strafeMag * Vector3.left;
|
|
|
|
if (Input.GetKey(KeyCode.D))
|
|
strafe += m_strafeMag * Vector3.right;
|
|
|
|
return strafe;
|
|
}
|
|
|
|
void processInput() {
|
|
updateCamera();
|
|
|
|
if (Input.GetKey(KeyCode.Space))
|
|
Respawn();
|
|
|
|
// if (Input.GetKey(KeyCode.Alpha1))
|
|
// m_forceMode = ForceMode.Acceleration;
|
|
// if (Input.GetKey(KeyCode.Alpha2))
|
|
// m_forceMode = ForceMode.Impulse;
|
|
// if (Input.GetKey(KeyCode.Alpha3))
|
|
// m_forceMode = ForceMode.Force;
|
|
// if (Input.GetKey(KeyCode.Alpha4))
|
|
// m_forceMode = ForceMode.VelocityChange;
|
|
|
|
if (Input.GetKey(KeyCode.F10))
|
|
Application.Quit();
|
|
}
|
|
|
|
public void Respawn() {
|
|
m_thrust = new Vector3(0.0f, 0.0f, 0.0f);
|
|
transform.position = m_initialPosition;
|
|
m_rigidBody.transform.forward = m_initialForce;
|
|
m_rigidBody.velocity = new Vector3(0f, 0f, 0f);
|
|
m_rigidBody.angularVelocity = new Vector3(0f, 0f, 0f);
|
|
m_camera.transform.rotation = m_initialCamera;
|
|
|
|
m_camPitch = m_initialCamera.eulerAngles.x;
|
|
m_camYaw = m_initialCamera.eulerAngles.y;
|
|
}
|
|
|
|
void updateCamera() {
|
|
m_camPitch -= m_mouseSenseY * Input.GetAxis("Mouse Y");
|
|
m_camYaw += m_mouseSenseX * Input.GetAxis("Mouse X");
|
|
m_camera.transform.eulerAngles = new Vector3(m_camPitch, m_camYaw, 0.0f);
|
|
}
|
|
|
|
void OnDrawGizmosSelected() {
|
|
if (Application.isPlaying == false)
|
|
return;
|
|
|
|
Gizmos.color = Color.red;
|
|
Vector3 right = m_camera.transform.TransformDirection(Vector3.right) * 10;
|
|
Gizmos.DrawRay(transform.position, right);
|
|
|
|
Gizmos.color = Color.green;
|
|
Vector3 direction = m_camera.transform.TransformDirection(Vector3.up) * 10;
|
|
Gizmos.DrawRay(transform.position, direction);
|
|
|
|
Gizmos.color = Color.blue;
|
|
Vector3 forward = m_camera.transform.TransformDirection(Vector3.forward) * 10;
|
|
Gizmos.DrawRay(transform.position, forward);
|
|
|
|
Gizmos.color = Color.yellow;
|
|
Vector3 velocity = m_rigidBody.velocity;
|
|
Gizmos.DrawRay(transform.position, velocity);
|
|
|
|
Gizmos.color = Color.cyan;
|
|
Vector3 thrust = m_thrust;
|
|
Gizmos.DrawRay(transform.position, thrust);
|
|
}
|
|
} |