- Home /
Twin Stick Shooter
I'm a casual Unity user and I'm starting a game project that will be at its base a twin stick shooter. If you're unfamiliar with the genre look at Geometry Wars and Beat Hazard, which are two good examples.
What I want to do though, is use a 360 controller and have the left stick move the player's avatar around the screen, and have the right stick just set the direction the player's avatar is looking (there won't be any shooting).
After looking around I'm fairly confident getting the left stick scripted should be fairly easy, but I have no idea on how to go about scripting the right stick. Any tutorials online, or examples anyone could point me two would be a huge help and greatly appreciated.
Answer by skovacs1 · Oct 01, 2010 at 09:03 PM
Not sure from memory if I've ever played a twin stick, but I assume from those two examples (which I never played) that it's something where you can move position with one stick and you can change direction with the other, such that orientation and movement are independent.
I would apply a script to the player's avatar that looks kind of like:
var moveSensitivity : float = 3.0;
function Update() { var lh : float = Input.GetAxis("LeftStickHorizontal"); var lv : float = Input.GetAxis("LeftStickVertical"); var rh : float = Input.GetAxis("RightStickHorizontal"); var rv : float = Input.GetAxis("RightStickVertical");
//Assumes you're looking down the z axis
transform.position += Vector3(lh, lv, 0.0).normalized * moveSensitivity
* Time.deltaTime;
//Assumes you're looking down the z axis and that you are looking down on the avatar
transform.LookAt(transform.position + Vector3(rh, rv, 0.0), -Vector3.forward);
}
This moves you based on the left stick input and turns your avatar to always face the direction input by the right stick input.
If your look control is meant to be relative rotations rather than specific directions, then you would instead use something like:
//Don't need right vertical
//Assumes a variable for rotation sensitivity
transform.eulerAngles.z -= rh * rotateSensitivity * Time.deltaTime;
That sounds about right, I will try it out. Thank you very much!
Alright I got the left and right stick set up by editing the input options and adding the RightStickHorizontal and RightStickVertical (the left stick works with just the default Horizontal and Vertical). The left stick works, but the rotation seems to be off. The avatar moves, but not directly with the input it seems.
If this helps any I am moving the character on the x and y axis only, and need it to rotate on the z axis only.
I forgot to add the current position to the LookAt if that helps, but couldn't login over the weekend to correct it if that helps - it has been corrected in the answer above.
It doesn't move "directly" with the input? I'm not sure what that means. All I can think of when I hear that is that GetAxis is a smoothed value between -1 and 1, representing the current position of the axis. To get the non-smoothed values, you would use GetAxisRaw.
If your motion isn't behaving properly around the diagonals, it's likely because the transform vector was not normalized - this has also been corrected.
Your answer
Follow this Question
Related Questions
Will it be possible to develop for the PSP vita? 0 Answers
Toggle Boolean with 360 Controller Face Buttons 3 Answers
xbox controller not mapping 0 Answers
How to map controls for any controller. 3 Answers
Making a rigidbody stick to its position 4 Answers