patterncsharpMinor
Rocket controller for a Unity game
Viewed 0 times
unitycontrollerrocketgamefor
Problem
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
```
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 ();
}
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 ();
}
Solution
UPDATE: Unity has built-in support for rebindable keys. I still urge you to setup rebindable keys, but a better way to do so would be through the system described by https://codereview.stackexchange.com/a/85568/35495.
One thing I really see happening with your code is that some poor gamer using a non-qwerty keyboard (like azerty or dvorak) will have an unplayable game because their keyboard has a different layout. for example, as an azerty-player I use ZQSD instead of WASD, and the W key is on the bottom line. In addition, there are players that use ESDF so they have a larger keyspace available for hotkeys.
The usual way of handling this is:
Make a .cfg or .ini file (basically a text file) in a config folder in your game
Put this code in there:
Then you load this into a dictionary when the game loads using a text file parser (plenty of examples for that online).
When the user then does any input, you check which button they pushed. Is it one of the configured buttons? then do the corresponding action. Is it a non-configured button? then return.
You can then use the default Unity launcher to configure these buttons, or you can configure these buttons using a custom config menu ingame.
One thing I really see happening with your code is that some poor gamer using a non-qwerty keyboard (like azerty or dvorak) will have an unplayable game because their keyboard has a different layout. for example, as an azerty-player I use ZQSD instead of WASD, and the W key is on the bottom line. In addition, there are players that use ESDF so they have a larger keyspace available for hotkeys.
The usual way of handling this is:
Make a .cfg or .ini file (basically a text file) in a config folder in your game
Put this code in there:
[Input]
button_W = forward;
button_S = back;
button_A = left;
button_D = right;
//any other buttons you need;Then you load this into a dictionary when the game loads using a text file parser (plenty of examples for that online).
When the user then does any input, you check which button they pushed. Is it one of the configured buttons? then do the corresponding action. Is it a non-configured button? then return.
You can then use the default Unity launcher to configure these buttons, or you can configure these buttons using a custom config menu ingame.
Code Snippets
[Input]
button_W = forward;
button_S = back;
button_A = left;
button_D = right;
//any other buttons you need;Context
StackExchange Code Review Q#85319, answer score: 6
Revisions (0)
No revisions yet.