- Home /
Tilt gameboard
I need some 'controls' help for a game that has the arrow keys title a 5x5 board. The pivot point needs to be the dead center of the board and every arrow should move a side (IE: press RIGHT, and the right edge of the board moves down). I mainly need help with the pivot action as I have done some movement with the arrow keys before, but this will be different and I need to be able to make it multi-touch (IE: pressing RIGHT & UP will tilt the board towards the right and top edge (from your perspective)).
Answer by Kleptomaniac · Apr 05, 2012 at 01:17 PM
Hey there,
Just use Input.GetAxis (Horizontal and Vertical). GetAxis returns float values in the range of -1 to 1, so we can use that horizontal and vertical rotation variables to tilt the game board.
Try something like this:
var minRotation : float; //Needs to be within a range of 0 and 85
var maxRotation : float; //Needs to be within a range of 0 and -85
function Update () {
var horRot : float = (Input.GetAxis("Horizontal")) * Time.deltaTime * -20;
var vertRot : float = (Input.GetAxis("Vertical")) * Time.deltaTime * 20;
var totalHorRot : float;
var totalVertRot : float;
if (transform.eulerAngles.x + horRot < 180) {
totalHorRot = transform.eulerAngles.x + horRot;
} else {
totalHorRot = transform.eulerAngles.x + horRot - 360;
}
if (transform.eulerAngles.z + vertRot < 180) {
totalVertRot = transform.eulerAngles.z + vertRot;
} else {
totalVertRot = transform.eulerAngles.z + vertRot - 360;
}
transform.rotation = Quaternion.Euler(Mathf.Clamp(totalHorRot, minRotation, maxRotation), 0, Mathf.Clamp(totalVertRot, minRotation, maxRotation));
}
Attach this script to your game board and you're good to go. :)
Hope that helps! Klep
EDIT
Here is the C# version without resetting to 0. I'll update the Javascript version as well:
public float minRotation; //Needs to be within a range of 0 and 85
public float maxRotation; //Needs to be within a range of 0 and -85
void Update () {
float horRot = (Input.GetAxis("Horizontal")) * Time.deltaTime * -20;
float vertRot = (Input.GetAxis("Vertical")) * Time.deltaTime * 20;
float totalHorRot;
float totalVertRot;
if (transform.eulerAngles.x + horRot < 180) {
totalHorRot = transform.eulerAngles.x + horRot;
} else {
totalHorRot = transform.eulerAngles.x + horRot - 360;
}
if (transform.eulerAngles.z + vertRot < 180) {
totalVertRot = transform.eulerAngles.z + vertRot;
} else {
totalVertRot = transform.eulerAngles.z + vertRot - 360;
}
transform.rotation = Quaternion.Euler(Mathf.Clamp(totalHorRot, minRotation, maxRotation), 0, Mathf.Clamp(totalVertRot, minRotation, maxRotation));
}
Sorry, completely forgot that the pivot point of a mesh is default in the dead centre, so no need for an empty gameObject. Edited answer :)
Wow, thanks, I'm glad I wasn't forgotten about :D. However, I'm getting a fist full of errors now. O.o The ":" and "float" is 'unexpected'. And I can't get past those to see if there's anything else :(.
Also, in order to control the tilt (like pressing an arrow key) do I just do something like this?
if (Input.Get$$anonymous$$eyDown("right")) { horRot + 1; //or horRot++ }
Actually I could probably try converting to C# if you wanted to ... not my native language so it'll be touch and go ... I'll give it a go ...
Done. This works in C# and doesn't reset to 0. Took me a while to get past heaps of annoying $$anonymous$$athf.Clamp issues. :)
void Update () {
float horRot = (Input.GetAxis("Horizontal")) * Time.deltaTime * -20;
float vertRot = (Input.GetAxis("Vertical")) * Time.deltaTime * 20;
float totalHorRot;
float totalVertRot;
if (transform.eulerAngles.x + horRot < 350) {
totalHorRot = transform.eulerAngles.x + horRot;
} else {
totalHorRot = transform.eulerAngles.x + horRot - 360;
}
if (transform.eulerAngles.z + vertRot < 350) {
totalVertRot = transform.eulerAngles.z + vertRot;
} else {
totalVertRot = transform.eulerAngles.z + vertRot - 360;
}
transform.rotation = Quaternion.Euler($$anonymous$$athf.Clamp(totalHorRot, -5.5f, 5.5f), 0, $$anonymous$$athf.Clamp(totalVertRot, -5.5f, 5.5f));
}
Yeah, that's something to do with my conversion of the rotation into negative numbers. I can't for the life of me figure out how to fix it, so what I would suggest is ins$$anonymous$$d of using -90 and 90, use -85 and 85. :) Then it works properly.
Just hinting, but I'd love an upvote for this one ... it's taken me a lot, including learning C# syntax ... :D