162 lines
4.7 KiB
C#
Executable File
162 lines
4.7 KiB
C#
Executable File
using UnityEngine;
|
|
|
|
public class ShipControls : MonoBehaviour {
|
|
public Camera m_camera;
|
|
|
|
Rigidbody m_rigidBody;
|
|
|
|
Vector3 m_initialPosition;
|
|
Vector3 m_initialForce;
|
|
|
|
Vector3 m_thrust;
|
|
Vector3 m_strafe;
|
|
|
|
float m_strafeMag;
|
|
float m_thrustMag;
|
|
float m_stabliseMag;
|
|
ForceMode m_forceMode;
|
|
|
|
// Camera Controls
|
|
float m_mouseH = 0.5f;
|
|
float m_mouseV = 0.5f;
|
|
float m_camYaw = 0.0f;
|
|
float m_camPitch = 0.0f;
|
|
|
|
void Start() {
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
m_rigidBody = GetComponent<Rigidbody>();
|
|
|
|
m_initialPosition = m_rigidBody.transform.position;
|
|
m_initialForce = Vector3.zero;
|
|
|
|
m_thrust = Vector3.zero;
|
|
m_strafe = Vector3.zero;
|
|
|
|
m_thrustMag = 50.0f;
|
|
m_strafeMag = 25.0f;
|
|
m_stabliseMag = 75.0f;
|
|
|
|
m_forceMode = ForceMode.Acceleration;
|
|
// m_rigidBody.freezeRotation = true;
|
|
}
|
|
|
|
void Update() {
|
|
// update the movement vectors every frame
|
|
m_thrust = getThrust();
|
|
m_strafe = getStrafe();
|
|
|
|
// if (m_thrust != Vector3.zero)
|
|
// Debug.Log(m_thrust);
|
|
|
|
// 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
|
|
if (Input.GetMouseButton(0))
|
|
return m_camera.transform.TransformDirection(Vector3.forward) * m_thrustMag;
|
|
|
|
// brake
|
|
if (Input.GetMouseButton(1)) {
|
|
return m_rigidBody.velocity.normalized * -1f * m_thrustMag;
|
|
}
|
|
|
|
return Vector3.zero;
|
|
}
|
|
|
|
// 2D strafing
|
|
// TODO add multiplier
|
|
// combine
|
|
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();
|
|
}
|
|
|
|
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_camera.transform.eulerAngles = Vector3.zero;
|
|
|
|
m_camYaw = 0f;
|
|
m_camPitch = 0f;
|
|
}
|
|
|
|
void updateCamera() {
|
|
m_camYaw += m_mouseH * Input.GetAxis("Mouse X");
|
|
m_camPitch -= m_mouseV * Input.GetAxis("Mouse Y");
|
|
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);
|
|
}
|
|
} |