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
1
Question by Arycama · Jul 01, 2014 at 05:11 PM · inputmousejoystickmouse positionflight

How can I treat mouse movement as joystick input?

Is there a way to make mouse movement act like a joystick, including the dead zone? So, if the mouse is moved away from the middle of the screen I can make an object move?

For example, I have a space ship and if the mouse is in (or near) the middle of the screen, it will keep facing forward. But if it is moved to the left/right, or top/bottom of the screen, the ship will start rotating. (And will keep rotating until the mouse is returned close to the middle. (There will probably be an onscreen crosshair or similar indicating the mouse position)

Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Arycama · Jul 02, 2014 at 08:51 AM

Actually nevermind, I figured out an easy way to achieve what I wanted. For anyone else interested, I simply made two float variables within the movement script, called "mousePosX" and "mousePosY", and set them to 0. Then in the Update function, I set mousePosX += Input.GetAxis("Mouse X"), and the same for mouse Y, which I then used to control adding torque along the x and y axes.

Thanks for the help!

Comment
Add comment · Show 1 · 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 TaylorMatson · May 07, 2016 at 06:19 AM 0
Share

I am trying to replicate this but I am unable to add my flat to get axis..

using UnityEngine; using System.Collections;

public class CubeControl : $$anonymous$$onoBehaviour {

 // Use this for initialization
 void Start () {

     float mousePosX = 0;
     float mousePosY = 0;
 }

 // Update is called once per frame
 void Update () {

     float mousePosX = Input.GetAxis ("$$anonymous$$ouse X") + mousePosX;
     float mousePosY = Input.GetAxis("$$anonymous$$ouse Y") + mousePosY;

     var x = Input.GetAxis ("$$anonymous$$ouse X");
     var y = Input.GetAxis ("$$anonymous$$ouse Y");

     transform.Rotate (mousePosX, 0f, mousePosY);

 }

}

avatar image
1

Answer by tanoshimi · Jul 01, 2014 at 05:37 PM

Yes, you can, using Input.mousePosition.

Comment
Add comment · Show 1 · 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 Arycama · Jul 02, 2014 at 08:53 AM 0
Share

Thanks, it doesn't work exactly as I'd like though. $$anonymous$$y current input is basically pitch = Input.getAxis("$$anonymous$$ouse Y") yaw = Input.getAxis("$$anonymous$$ouse X"), then Rigidbody.addRelativeTorque(pitch, yaw, roll). If I connect a game pad or joystick and map it to the pitch and yaw variables, I get smooth consistent movement when it is pressed. However, with a mouse it requires constant moving.

Input.mousePosition would require a lot of extra scripting for a deadzone and lots of greater than/less than checks to see what direction it moves. However, I'll keep working with Input.mousePosition to try and figure out a solution, unless someone else can chip in.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Simulate a mouse Click? 2 Answers

How could I use the mouse as input for a flight control system (and space bar for thrust!) 2 Answers

Implementing a virtual joystick with mouse 1 Answer

Disable mouse input and cursor in game 2 Answers

Keyboard/Joystick Inputs Do Not Work until Mouse Clicked 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