Hello everyone recently I’ve been working on my first game(space shooter) and I’ve published it on gamejolt. It’s going great so far, but a few of the people complained that the controls are hard. As it stands my controls are setup in the following way:
W,S - make the player move forward and backward.
A,D - make the player rotate left/right
Now what I want is to introduce the buttons Q and E and when pressed I want the player to move, sideways not rotate. My current code doesn’t have an applied rigid body but I’ve tried applying one and then saying something like:
if (Input.GetKey (KeyCode.E))
{
rb2d.AddForce (transform.right * speed);
}
But that didn’t do anything so here is my current code without my attempt to introduce a Rigidbody:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float maxSpeed = 5.0f;
public float rotSpeed = 140f;
public Rigidbody2D rb2d;
float shipBoundaryRadius = 0.5f;
void Start () {
rb2d = gameObject.GetComponent <Rigidbody2D> ();
}
void Update () {
Quaternion rot = transform.rotation;
float z = rot.eulerAngles.z;
z -= Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
rot = Quaternion.Euler( 0, 0, z );
transform.rotation = rot;
Vector3 pos = transform.position;
Vector3 velocity = new Vector3(0, Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime, 0);
pos += rot * velocity;
if(pos.y+shipBoundaryRadius > Camera.main.orthographicSize) {
pos.y = Camera.main.orthographicSize - shipBoundaryRadius;
}
if(pos.y-shipBoundaryRadius < -Camera.main.orthographicSize) {
pos.y = -Camera.main.orthographicSize + shipBoundaryRadius;
}
float screenRatio = (float)Screen.width / (float)Screen.height;
float widthOrtho = Camera.main.orthographicSize * screenRatio;
if(pos.x+shipBoundaryRadius > widthOrtho) {
pos.x = widthOrtho - shipBoundaryRadius;
}
if(pos.x-shipBoundaryRadius < -widthOrtho) {
pos.x = -widthOrtho + shipBoundaryRadius;
}
transform.position = pos;
}
}
P.S If you want to see the game: Game Jolt - Share your creations