deceleration boiiii
This commit is contained in:
1 parent
b009d4fe5a
commit
2cc43a3515
4 files changed
+63
-120
No files matched your search
+53
-23
@@ -3,7 +3,7 @@ using UnityEngine;
|
||||
public class ShipControls : MonoBehaviour {
|
||||
public Camera m_camera;
|
||||
|
||||
Rigidbody m_ridigBody;
|
||||
Rigidbody m_rigidBody;
|
||||
|
||||
Vector3 m_initialPosition;
|
||||
Vector3 m_initialForce;
|
||||
@@ -16,12 +16,17 @@ public class ShipControls : MonoBehaviour {
|
||||
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_ridigBody = GetComponent<Rigidbody>();
|
||||
m_rigidBody = GetComponent<Rigidbody>();
|
||||
|
||||
m_initialPosition = m_ridigBody.transform.position;
|
||||
m_initialPosition = m_rigidBody.transform.position;
|
||||
m_initialForce = Vector3.zero;
|
||||
|
||||
m_thrust = Vector3.zero;
|
||||
@@ -32,6 +37,7 @@ public class ShipControls : MonoBehaviour {
|
||||
m_stabliseMag = 75.0f;
|
||||
|
||||
m_forceMode = ForceMode.Acceleration;
|
||||
// m_rigidBody.freezeRotation = true;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
@@ -39,7 +45,8 @@ public class ShipControls : MonoBehaviour {
|
||||
m_thrust = getThrust();
|
||||
m_strafe = getStrafe();
|
||||
|
||||
Debug.Log(m_thrust);
|
||||
// if (m_thrust != Vector3.zero)
|
||||
// Debug.Log(m_thrust);
|
||||
|
||||
// do everything else input related on every frame too
|
||||
processInput();
|
||||
@@ -47,18 +54,16 @@ public class ShipControls : MonoBehaviour {
|
||||
|
||||
void FixedUpdate() {
|
||||
// // the ship being propelled forward
|
||||
m_ridigBody.AddForce(m_thrust, m_forceMode);
|
||||
m_rigidBody.AddForce(m_thrust, m_forceMode);
|
||||
|
||||
// moving side to side
|
||||
m_ridigBody.AddForce(m_strafe, m_forceMode);
|
||||
|
||||
// add relative force?
|
||||
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_ridigBody.rotation = Quaternion.RotateTowards(transform.rotation, m_camera.transform.rotation, step);
|
||||
m_rigidBody.rotation = Quaternion.RotateTowards(transform.rotation, m_camera.transform.rotation, step);
|
||||
}
|
||||
|
||||
Vector3 getThrust() {
|
||||
@@ -67,8 +72,9 @@ public class ShipControls : MonoBehaviour {
|
||||
return m_camera.transform.TransformDirection(Vector3.forward) * m_thrustMag;
|
||||
|
||||
// brake
|
||||
if (Input.GetMouseButton(1))
|
||||
return m_camera.transform.TransformDirection(Vector3.back) * m_thrustMag;
|
||||
if (Input.GetMouseButton(1)) {
|
||||
return m_rigidBody.velocity.normalized * -1f * m_thrustMag;
|
||||
}
|
||||
|
||||
return Vector3.zero;
|
||||
}
|
||||
@@ -80,20 +86,22 @@ public class ShipControls : MonoBehaviour {
|
||||
Vector3 strafe = new Vector3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
if (Input.GetKey(KeyCode.W))
|
||||
strafe += m_strafeMag * m_camera.transform.TransformDirection(Vector3.up);
|
||||
strafe += m_strafeMag * Vector3.up;
|
||||
if (Input.GetKey(KeyCode.S))
|
||||
strafe += m_strafeMag * m_camera.transform.TransformDirection(Vector3.down);
|
||||
strafe += m_strafeMag * Vector3.down;
|
||||
|
||||
if (Input.GetKey(KeyCode.A))
|
||||
strafe += m_strafeMag * m_camera.transform.TransformDirection(Vector3.left);
|
||||
strafe += m_strafeMag * Vector3.left;
|
||||
|
||||
if (Input.GetKey(KeyCode.D))
|
||||
strafe += m_strafeMag * m_camera.transform.TransformDirection(Vector3.right);
|
||||
strafe += m_strafeMag * Vector3.right;
|
||||
|
||||
return strafe;
|
||||
}
|
||||
|
||||
void processInput() {
|
||||
updateCamera();
|
||||
|
||||
if (Input.GetKey(KeyCode.Space))
|
||||
Respawn();
|
||||
|
||||
@@ -105,28 +113,50 @@ public class ShipControls : MonoBehaviour {
|
||||
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_ridigBody.transform.forward = m_initialForce;
|
||||
m_ridigBody.velocity = new Vector3(0f, 0f, 0f);
|
||||
m_camera.transform.eulerAngles = Vector3.forward;
|
||||
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() {
|
||||
Gizmos.color = Color.red;
|
||||
Vector3 direction = m_camera.transform.TransformDirection(Vector3.up) * 10;
|
||||
Gizmos.DrawRay(transform.position, direction);
|
||||
if (Application.isPlaying == false)
|
||||
return;
|
||||
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.color = Color.red;
|
||||
Vector3 right = m_camera.transform.TransformDirection(Vector3.right) * 10;
|
||||
Gizmos.DrawRay(transform.position, right);
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user