A more complex controller

DISCLAIMER:
While I encourage the use of more advanced controls in this game, I do not encourage you copying and pasting this code just to get through the course. I learned a LOT in the few hours it took to build this, so please do build your own controller or take time to understand what I’m doing here. You’re only hurting yourself if you just copy and paste.

So, when challenged… I, of course, went WAY too far with the PlayerController script… but I got it working, and it works well.

using UnityEngine;
using System.Collections;
using System.Security.Cryptography;

public class PlayerController : MonoBehaviour {

bool leftPress = false;
bool rightPress = false;
bool upPress = false;
bool downPress = false;
bool firePress = false;
public float speed = 1.0f;

// Update is called once per frame
void Update ()
{
//left & right controller start
if (Input.GetKey (KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
leftPress = true;
} else {
leftPress = false;
}
print (leftPress);

if (Input.GetKey (KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
rightPress = true;

} else {
rightPress = false;
}
print (rightPress);
// left & right controller end

// up & down controller start
if (Input.GetKey (KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
upPress = true;
} else {
upPress = false;
}
print (upPress);

if (Input.GetKey (KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) {
downPress = true;

} else {
downPress = false;
}
print (downPress);
// up & down controller end

//fire control start
if (Input.GetKey (KeyCode.Space) || Input.GetKey (KeyCode.RightControl)) {
firePress = true;
} else {
firePress = false;
}
print (firePress);
//fire control end

MoveShip ();
}

void MoveShip ()
{
if (leftPress) {
this.transform.position += new Vector3 (-speed, 0, 0);
print ("Move Left"); 
}

if (rightPress) {
this.transform.position += new Vector3 (speed, 0, 0); 
}

if (upPress) {
this.transform.position += new Vector3 (0, speed, 0);
}

if (downPress) {
this.transform.position += new Vector3 (0, -speed, 0);
}


}

}

The fire control is built in because I figure, eventually, I’ll need to shoot stuff. It doesn’t do anything yet, but the move keys fully function and the ship can be controlled with both wasd and arrow keys.

In my research, I did see a few posts about how to control players being able to double their speed output if they use both keys, and I have not guarded against that in any way, so you may need to consider that.

Hope this helps someone.

EDIT: Also, I’m not quite sure why my code includes:

using System.Security.Cryptography;

:slight_smile: I am about to mention that on the other thread we have been replying on. You can remove this :slight_smile:

UPDATE:

Here is an update to the code. It now clamps movement to within the viewport:

using UnityEngine;
using System.Collections;
using System;

public class PlayerController : MonoBehaviour {

bool leftPress = false;
bool rightPress = false;
bool upPress = false;
bool downPress = false;
bool firePress = false;
public float speed = 12.0f;
float xmin;
float xmax;
float ymin;
float ymax;

void Start() {
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint (new Vector3(0,0,distance));
Vector3 rightmost = Camera.main.ViewportToWorldPoint (new Vector3(1,0,distance));
xmin = leftmost.x + .52f;
xmax = rightmost.x - .52f;

Vector3 topmost = Camera.main.ViewportToWorldPoint (new Vector3(0,0,distance));
Vector3 bottommost = Camera.main.ViewportToWorldPoint (new Vector3(1,1,distance));
ymin = topmost.y + .52f;
ymax = bottommost.y - .52f;

}

// Update is called once per frame
void Update ()
{
//left & right controller start
if (Input.GetKey (KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
leftPress = true;
} else {
leftPress = false;
}


if (Input.GetKey (KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
rightPress = true;

} else {
rightPress = false;
}

// left & right controller end

// up & down controller start
if (Input.GetKey (KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
upPress = true;
} else {
upPress = false;
}


if (Input.GetKey (KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) {
downPress = true;

} else {
downPress = false;
}
// up & down controller end

//fire control start
if (Input.GetKey (KeyCode.Space) || Input.GetKey (KeyCode.RightControl)) {
firePress = true;
} else {
firePress = false;
}
//fire control end

MoveShip ();

float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint (new Vector3(0,0,distance));
Vector3 rightmost = Camera.main.ViewportToWorldPoint (new Vector3(1,0,distance));
Vector3 topmost = Camera.main.ViewportToWorldPoint (new Vector3(0,0,distance));
Vector3 bottommost = Camera.main.ViewportToWorldPoint (new Vector3(1,0,distance));
}

void MoveShip ()
{
if (leftPress) {
this.transform.position += new Vector3 (-speed * Time.deltaTime, 0, 0);
// transform.position += Vector3.left/right/up/down * speed * Time.deltaTime;          <- can also use for ^
}

if (rightPress) {
this.transform.position += new Vector3 (speed * Time.deltaTime, 0, 0); 
}

if (upPress) {
this.transform.position += new Vector3 (0, speed * Time.deltaTime, 0);
}

if (downPress) {
this.transform.position += new Vector3 (0, -speed * Time.deltaTime, 0);
}

// restrict the player to the gamespace
float newX = Mathf.Clamp (transform.position.x, xmin, xmax);
float newY = Mathf.Clamp (transform.position.y, ymin, ymax); 
transform.position = new Vector3 (newX, newY, transform.position.z);
}
}

Again, this does not account for users possibly being able to double their speed if using both keys at once. I’d assume that since I use OR statements ( || ) the user won’t be able to do this, but I haven’t tested it yet.
EDIT: I tested this just now. If you try to use an arrow key and it’s corresponding wasd key, movement completely stops due to the || statement.

The syntax here will restrict movement within the screenview, meaning that the player can control the ship and move it around at any resolution. This means that, even though this course teaches using an 800x600 scale, you can set your resolution to 1920x1080 and the ship should still stay within the bounds of the screen.

2 Likes

I’ve had to append these lines when trying to increase the resolution in the game from the suggested 800x600 to a higher res:

xmin = leftmost.x + .52f;
xmax = rightmost.x - .52f;
ymin = topmost.y + .52f;
ymax = bottommost.y - .52f;

For me, using 1024x768, the .52f changes as such:

xmin = leftmost.x + .01f;
xmax = rightmost.x - .97f;
ymin = topmost.y + .01f;
ymax = bottommost.y - .97f;

I’d assume the numbers would scale accordingly if using 1920x1080. I’ll try that eventually, but maybe not in Laser Defender.

Privacy & Terms