- Home /
How to shoot using the right mobile Joystick?
Hi all, I have been looking to answer for a long time, haven't been able to find anything. I want to make a game with 2 joysticks, but I don't know how to make the player face the direction of my joystick. My game is top down shooter, so the player is on the x and z axis. I am also having trouble with the bullet instantiating. They just keep coming. Is there a way to make a pause between them so there isn't just a stream of bullets? Thanks in advance.
I am currently having the same problem, would really appreciate at least a hint.
my code so far: #pragma strict
var speed : float = 10.0;
var rotateSpeed : float = 100.0;
var moveJoystick : Joystick;
var rotateJoystick : Joystick;
var bullet: GameObject;
var shootAgain: float = 0;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
shootAgain += Time.deltaTime;
// Rotate around y - axis
var rotatePos = Input.GetAxis ("Horizontal") ?
Input.GetAxis ("Horizontal") : joyStickInput(rotateJoystick);
this.transform.Rotate(0, rotatePos * rotateSpeed, 0);
if(shootAgain >= 0){
if(rotatePos > 0.5){
//bullet = Instantiate(bullet, this.transform.position, this.transform.rotation);
fire();
}
if(rotatePos < -0.5){
//bullet = Instantiate(bullet, this.transform.position, this.transform.rotation);
fire();
}
}
/*// $$anonymous$$ove forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var movePos = Input.GetAxis ("Vertical") ?
Input.GetAxis ("Vertical") : joyStickInput(moveJoystick);
var curSpeed = speed * movePos;
controller.Simple$$anonymous$$ove(forward * curSpeed);
*/
}
function joyStickInput (joystick : Joystick) {
var absJoyPos = Vector2 ($$anonymous$$athf.Abs(joystick.position.x),
$$anonymous$$athf.Abs(joystick.position.y));
var xDirection = (joystick.position.x > 0) ? 1 : -1;
var yDirection = (joystick.position.y > 0) ? 1 : -1;
return ( ( absJoyPos.x > absJoyPos.y) ? absJoyPos.x * xDirection : absJoyPos.y * yDirection);
}
function fire () {
bullet = new Instantiate(bullet, this.transform.position, transform.rotation);
shootAgain = -0.5;
}
@script RequireComponent(CharacterController)
Answer by robertbu · Mar 20, 2014 at 12:48 AM
Facing the direction of a joystick and creating a pause between bullets are two separate questions. I recommend posting your bullet question separately, or doing some searches. The firing rate question has been asked and answered on many times on UA.
As for the joystick part of your question, you can cause your character to face the direction of the joystick by:
var x = Input.GetAxis("Horizontal");
var y = Input.GetAxis("Vertical");
if (x != 0.0 || y != 0.0) {
var angle = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(90.0 - angle, Vector3.up);
}
Substitute the appropriate axes for the specific joystick for 'Horizontal' and 'Vertical'.
it gives the error
BCE0022: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'.
I fixed the problem in the code fragment above, then ran a quick test to verify that it works.
wat do u mean by
Substitute the appropriate axes for the specific joystick for 'Horizontal' and 'Vertical'.
Joysticks can refer to different things. They may be virtual joysticks or physical joysticks for example. The Input.GetAxis() calls gets data from real devices...joysticks, keys and/or the mouse. When your question says "I want to make a game with 2 joysticks," if you are talking about physical joysticks, then you will have to setup axes for each joystick. On a physical joystick, there are two axes...a right/left axis and a up/down axis. So any code you write like your rotation code above that wants 360 angles needs to combine input the two axes.
The "Horizontal" and "Vertical" axes are setup for you. By default they are assigned to the arrow keys and the asdw keys. You can go to Edit > Project Settings > Input and change them to Joystick input. Alternately you can define new axes with new names to take input from your physical devices. Assu$$anonymous$$g you set these up, you would use your newly defined axes ins$$anonymous$$d of "Horizontal" or "Vertical."
Just to verify the code, I want you to create a new scene, create a cube, and attach this script:
function Update() {
var x = Input.GetAxis("Horizontal");
var y = Input.GetAxis("Vertical");
if (x != 0.0 || y != 0.0) {
var angle = $$anonymous$$athf.Atan2(y, x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(90.0 - angle, Vector3.up);
}
}
Assu$$anonymous$$g you have not change the settings for "Horizontal" or "Vertical," you will be able to use the arrow keys to rotate the block. You can use a combination of arrow keys as well.
I've never used the mobile Joysticks, but take a quick look at the script, it appears that the 'position' parameter is like GetAxis() in that it returns a value between -1 and 1 for both x and y. Assu$$anonymous$$g that is correct, you could do:
var x = moveJoystick.position.x;
var y = moveJoystick.position.y;
Then use x and y as I have to calculate an angle.
Answer by rockyourteeth · Mar 20, 2014 at 02:22 AM
I solved this problem in one of my games in an unconventional way. Robertbu's method is probably more correct, but what I did worked:
-Save my position at the beginning of the update to a variable (origPos)
-Translate using the "Horizontal" and "Vertical" axes
-Save the new position to a variable (targetPos)
-Set my position back to origPos
-Get a Vector3 with the difference of the two (targetPos - origPos)
-Normalize that vector3
-Then set my "forward" direction to that vector3
could you please give me the code? Im still new at this....
something like this, in C#:
Vector3 pos = transform.position;
transform.Translate(new Vector3(-Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f), Space.World);
Vector3 controllerTargetPos = transform.position;
Vector3 fwd = controllerTargetPos - pos;
fwd.Normalize();
transform.up = fwd; // you may need to use something other than "up", depending on what direction your character's "front" is
transform.position = pos;
transform.rotation = Quaternion.Euler(0.0f, 0.0f, transform.eulerAngles.z); // I added this line to avoid a weird spin I was getting at some angles. You may not need it, I'm not sure what was causing it exactly
how would I input mobile joysticks with this code?
I don't know how your mobile joysticks work. Somehow, they need to be connected in the input settings to "Horizontal" and "Vertical". Try Googling "Unity mobile joystick".
Again, I think robertbu's method is the more correct one. You just need to figure out how to get the right "get axis". Good luck!
Your answer
Follow this Question
Related Questions
How to make Dual Joysticks for Android and IOS? 1 Answer
How do I make on screen buttons in Android? 4 Answers
GUI Joystick 1 Answer
how to shoot with joystick 0 Answers
How do I rotate a character with joystick input in an isometric view? 1 Answer