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)).
1 Answer
1Hey 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 :)
– KleptomaniacHmm, I can't see why you would be getting those errors ... I didn't get any ... You aren't using it in a C# script are you? And I'm not sure what you mean by 'in order to control the tilt' ... That script already does that? Do you mean make it so it doesn't return to 0 when the horizontal or vertical axes aren't receiving input?
– KleptomaniacWell, there's your problem! :D Chuck here in a .js file and your good to go! :P Does it work for you?
– KleptomaniacActually 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 ...
– KleptomaniacI think that's it converted (not so sure if void is correct in this case): void Update () { float horRot = (Input.GetAxis("Horizontal")) * Time.deltaTime * -360; horRot = Mathf.Clamp(horRot, -5.5, 5.5); float vertRot = (Input.GetAxis("Vertical")) * Time.deltaTime * 360; vertRot = Mathf.Clamp(vertRot, -5.5, 5.5); transform.rotation = Quaternion.Euler(vertRot, 0, horRot); } Hope that works for you ...
– Kleptomaniac