I'm currently learning C# so I can make real games with Unity. This is the first C# program I've built in Unity. Essentially what is does is control the thrust, and rotation of a GameObject
shaped like a rocket. It may not necessarily be "real physics" but for my first program, it's good enough.
using UnityEngine;
using System.Collections;
public class RocketController : MonoBehaviour
{
public float thrustMultiplier;
public float rotationSpeed;
private bool applyThrust = false;
void Start () { transform.forward = transform.up; }
// Check for misc keypresses
void CheckMiscKeys ()
{
// Start applying thrust
if (Input.GetKey (KeyCode.Space))
{
applyThrust = true;
}
// Stop applying thrust
if (Input.GetKey (KeyCode.LeftShift))
{
applyThrust = false;
}
}
// Check for rotation keypresses
void CheckRotationKeys ()
{
// Rotate forward
if (Input.GetKey (KeyCode.W))
{
transform.Rotate (rotationSpeed * new Vector3 (1, 0, 0));
}
// Rotate backwards
if (Input.GetKey (KeyCode.S))
{
transform.Rotate (rotationSpeed * new Vector3 (-1, 0, 0));
}
// Rotate left
if (Input.GetKey (KeyCode.A))
{
transform.Rotate (rotationSpeed * new Vector3 (0, -1, 0));
}
// Rotate right
if (Input.GetKey (KeyCode.D))
{
transform.Rotate (rotationSpeed * new Vector3 (0, 1, 0));
}
}
// Apply thrust to the rocket's rigidbody
void ApplyRocketThrust ()
{
if (applyThrust)
{
Vector3 force = transform.forward * thrustMultiplier;
rigidbody.AddForce(force);
}
}
// Run physics calculations and misc events
void FixedUpdate ()
{
CheckMiscKeys ();
CheckRotationKeys ();
ApplyRocketThrust ();
}
}
What can be improved? Is there anything I'm doing wrong here?