• 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
Question by Selosoft · Sep 17, 2012 at 06:27 AM · positionmousejoystickgamepadcrosshair

Use Gamepad right analog stick instead of mouse to control crosshair movement

I'm trying to figure out how I can use the right analog stick of my gamepad to control the movement of my mouse cursor instead of using the mouse. Is t$$anonymous$$s possible? I tried changing the mouse horizontal and vertical axes in the input manager to use joystick axis 3 and 4 but not$$anonymous$$ng happens. The mouse still works and the analog stick is not recognized.

A little more info if anyone has any suggestions: It's a 2d game and when the character collects a gun I'm trying to use the right analog stick as a crosshair that you can move anywhere on the screen to shoot. I replace the mouse cursor with a gui texture and just need to get the analog stick to replace the mouse movement so the analog stick position is equal to the mouse position coordinates. Any tips welcome. Thanks!

Comment

People who like this

0 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 Seth-Bergman · Sep 17, 2012 at 06:36 AM 0
Share

for that to work, you also need to change the "Type" field to Joystick Axis (in the Input Manager)

avatar image Selosoft · Sep 17, 2012 at 04:55 PM 0
Share

yup I've done that but the joystick still doesn't work. I've adjusted the dead zone and sensitivity as well. Thanks for the suggestion though.

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Kuro Okami · Jan 04, 2014 at 05:38 AM

go to input manager, duplicate 'mouse x' and 'mouse y' and change the type values and the axis' on the duplicates, feel free to add a description saying 'joystick y' or 'joystick x' in the description section.

the reason for t$$anonymous$$s is that the mouselook script only refers to 'mouse x' or 'mouse y' . Chances are you are only going to use either the mouse or the gamepad so two of the same shouldn't cause problems.

Comment
alex31016
Chiaro22
Ash_88mph
Xeryuz
InsertNamehere
abhishek_hingorani
KUFgoddess
dinuka_jay

People who like this

8 Show 0 · 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

Answer by Itinerant · Sep 17, 2012 at 08:55 PM

I was just trying to figure t$$anonymous$$s one out too, using an xbox controller. My method was to create two new axis for the joystick. Make sure you choose w$$anonymous$$ch joystick you're using (for the xbox controller, it's joystick axis 4 and 5). Gravity/Dead/Sensitivity to 1/.2/1

Since I wanted the option of using the mouse as well, I changed the mouselook script to accept mouse or joystick input. If it's useful, here's the script, but you shouldn't need it if you just want to use the stick. Oh, except you need to go into the script and rename the 'Mouse X' and 'Mouse Y' to whatever you name your new axis.

 using UnityEngine;
 using System.Collections;
 
 /// MouseLook rotates the transform based on the mouse delta.
 /// Minimum and Maximum values can be used to constrain the possible rotation
 
 /// To make an FPS style character:
 /// - Create a capsule.
 /// - Add the MouseLook script to the capsule.
 ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
 /// - Add FPSInputController script to the capsule
 ///   -> A CharacterMotor and a CharacterController component will be automatically added.
 
 /// - Create a camera. Make the camera a c$$anonymous$$ld of the capsule. Reset it's transform.
 /// - Add a MouseLook script to the camera.
 ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
 [AddComponentMenu("Camera-Control/Mouse Look")]
 public class MouseLook : MonoBehaviour {
 
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float mousesensitivityX = 15F;
     public float mousesensitivityY = 15F;
     
     public float joysensitivityX = 3F;
     public float joysensitivityY = 3F;
 
     public float minimumX = -360F;
     public float maximumX = 360F;
 
     public float minimumY = -60F;
     public float maximumY = 60F;
 
     float rotationY = 0F;
     
 
 
     void Update ()
     {
         
         float Xon = Mathf.Abs (Input.GetAxis ("Joy X"));
         float Yon = Mathf.Abs (Input.GetAxis ("Joy Y"));
             
         if (axes == RotationAxes.MouseXAndY)
         {
             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mousesensitivityX;
             
             rotationY += Input.GetAxis("Mouse Y") * mousesensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
         }
         else if (axes == RotationAxes.MouseX)
         {
             if (Xon>.05){
                 transform.Rotate(0, Input.GetAxis("Joy X") * joysensitivityX, 0);
             }
             transform.Rotate(0, Input.GetAxis("Mouse X") * mousesensitivityX, 0);
         }
         else
         {
             if (Yon>.05){
                 rotationY += Input.GetAxis("Joy Y") * joysensitivityY;
             }
                 rotationY += Input.GetAxis("Mouse Y") * mousesensitivityY;
                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
                 transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
             
         }
     }
     
     void Start ()
     {
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 }
Comment
ytwithlove
fearlesshyena
Kraykon
KUFgoddess

People who like this

4 Show 3 · 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 ytwithlove · Jul 01, 2014 at 07:36 PM 1
Share

Old post but exactly what I was looking for! Thanks! ^_^

avatar image KUFgoddess · Dec 20, 2016 at 09:48 AM 0
Share

What if you're trying to do this in a 2D sidescrolling game. My mouse has a texture on it would i need to make a fake mouse and then use this script or anything like that? If you could ever so kindly fill me in I would appreciate it greatly

avatar image shajahan · May 09, 2017 at 09:46 AM 0
Share

Thanks brother

avatar image

Answer by Unshackled · Jan 18, 2017 at 03:49 PM

I eventually got my xbox right-thumbstick to act as a mouse/pointer using a different method.

Edit > project settings > input: I made 2 new input axis called xboxl + xboxr assigning the 4th and 5th axis and as joystick type.

I then added a new cube (adding a rigid body, turning gravity off) next to my player (player moves with left-thumbstick).

I added t$$anonymous$$s script to the cube

 using UnityEngine;
 using System.Collections;
 
 public class xboxrightstick : MonoBehaviour {
     public float speed = .5f;
     public float turnSpeed = 60;
     private Rigidbody rig;
     public float distance;
     public Transform target;
     public Transform mouseposition;
     public float maxdist;
     // Use t$$anonymous$$s for initialization
     void Start () {
         rig = GetComponent<Rigidbody>();
         maxdist = 10f;
     }
 
     void FixedUpdate() {
 
         Vector3 targetpos = target.transform.position;
     
         mouseposition = rig.transform;
         distance = (Vector2.Distance(transform.position, target.transform.position));
      
         if (Vector2.Distance(transform.position, target.transform.position) <= maxdist)
         {
             float rStickX = Input.GetAxis("xboxl");
             float rStickY = Input.GetAxis("xboxr");
 
             Vector3 movement = new Vector3(rStickX, rStickY, 0);
 
             rig.MovePosition(transform.position + movement / speed);
         }
         mouseposition = rig.transform;
     }  
 }
 

Now the cube will not go further than 10f from the player. Now all I have to do is make my player always look at the cube and then make the cube invisible. The player will then appear to be looking in the direction of your right-thumbstick input.

A small t$$anonymous$$ng I have to do is make the cube slowly move towards player other wise it gets stuck when it gets to distance of 10f from player.

Hope t$$anonymous$$s helps someone as the other methods I found online didn't work for me. It's not exactly a mouse but maybe its a step in the right direction for you. Could you simply put t$$anonymous$$s script on an object in the canvas to change the rect transform and then restrict its movement so it cant go off screen? I'm a beginner so i dont know if it would be that easy.

Comment

People who like this

0 Show 0 · 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

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

17 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

Related Questions

Keyboard/Joystick Inputs Do Not Work until Mouse Clicked 0 Answers

How do I access a gamepad's joystick position? 1 Answer

Using Gamepad to access onGUI menus? 0 Answers

How can I disable gamepad input, for my UI? 1 Answer

Gamepad input, axis OR button 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