• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by besieger1 · Nov 25, 2013 at 05:04 PM · controllerjoystickshooter

GamePad joystick

Hey guys i have been trying to create a dual stick shooter with the xbox controller or any for that matter.

Now I have got movement working fine but rotation doesn't work how I would like, when turning using the right joystick it seems to snap to certain angles like from 90 to 0 to 45 etc.

I have done some googling and found most people have the same problem...

 using UnityEngine;
 
 using System.Collections;
 
  
 
 public class DualStickShooter : MonoBehaviour {
     public float PlayerRotationSpeed = 1.0f;
     public float PlayerSpeed = 100.0f;
     
     public Transform bullet;
     public GameObject orginalBullet;
 
     // Update is called once per frame
 
     void Update () {
        // This assigns a float to each joystic axis (left and right analogue stick)
        // which is used as the input. This result is multiplied by the PlayerSpeed
        // and real time.
        float axisHorizontal = Input.GetAxis("1Horizontal") * PlayerSpeed * Time.deltaTime;
        float axisVertical = Input.GetAxis("1Vertical") * PlayerSpeed * Time.deltaTime;
         
        float axisHorizontal2 = Input.GetAxis("1FireHorizontal") * PlayerSpeed * Time.deltaTime;     
        float axisVertical2 = Input.GetAxis("1FireVertical") * PlayerSpeed * Time.deltaTime;        
        float translateX = axisHorizontal * PlayerSpeed * Time.deltaTime;
        float translateY = axisVertical * PlayerSpeed * Time.deltaTime;
 
        // move the player towards the joystick direction
        transform.Translate(translateX, 0, translateY);
        // rotate the player towards the joystick direction
        transform.localRotation = Quaternion.Euler(0.0f,(Mathf.Atan2(axisHorizontal2, axisVertical2) * Mathf.Rad2Deg), 0.0f);
 
  
        // shoot with left upper-bumper.
        if (Input.GetKeyDown("joystick button 6"))
        {
 
          shootBullet();
 
        }
     }
 
     void shootBullet()
 
     {
 
        Transform shot = Instantiate(bullet, transform.position, transform.rotation) as Transform;
 
        Physics.IgnoreCollision(shot.collider, collider); //Ignore collision from bullet to shooter.
 
     }
 
     void OnCollisionEnter()
     {
         rigidbody.velocity = Vector3.zero;
     }
     void OnCollisionExit()
     {
         rigidbody.velocity = Vector3.zero;
     }
     
     void ShootDelay()
     {
         //canShoot = true;
     }
 }
 
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

2D top down projectile with joystick problem 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Getting Joystick Input from Matricom G-Pad BX? 0 Answers

How do I check what input device is currently beeing used? 4 Answers

Joystick Controller works on Editor but not on Builded Game 0 Answers


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