Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pulkit8.mahajan · Mar 11, 2014 at 01:31 AM · mobileshootingjoysticktop down shooter

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DIllonJ93 · Mar 15, 2014 at 12:57 AM 0
Share

I am currently having the same problem, would really appreciate at least a hint.

avatar image pulkit8.mahajan · Mar 19, 2014 at 01:20 AM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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'.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pulkit8.mahajan · Mar 20, 2014 at 01:18 AM 0
Share

it gives the error

BCE0022: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'.

avatar image robertbu · Mar 20, 2014 at 03:16 AM 0
Share

I fixed the problem in the code fragment above, then ran a quick test to verify that it works.

avatar image pulkit8.mahajan · Mar 20, 2014 at 04:12 AM 0
Share

wat do u mean by

Substitute the appropriate axes for the specific joystick for 'Horizontal' and 'Vertical'.

avatar image robertbu · Mar 20, 2014 at 05:02 AM 1
Share

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.

avatar image robertbu · Mar 21, 2014 at 06:09 AM 1
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pulkit8.mahajan · Mar 20, 2014 at 02:29 AM 0
Share

could you please give me the code? Im still new at this....

avatar image rockyourteeth · Mar 20, 2014 at 04:16 AM 0
Share

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
 
avatar image pulkit8.mahajan · Mar 20, 2014 at 11:35 PM 0
Share

how would I input mobile joysticks with this code?

avatar image rockyourteeth · Mar 21, 2014 at 05:36 AM 0
Share

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".

avatar image rockyourteeth · Mar 21, 2014 at 05:37 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges