Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by NFNProductions · Mar 30, 2017 at 09:50 PM · mousepositionlockcursor

How can I move the mouse position whilst key is pressed?

I'm using Unity 5.5.2 and even if I create a new Unity project, in the editor, if I hold down a key (any key), then my mouse position locks and I cannot move it.

I want my player to move towards my mouse position (no click) and I also want to be able to hold down a key (e.g. space bar) to continuously fire. When my key is held down, the mouse does not move. Try it, just create a new Unity project, hit run and hold down a key, mouse is locked!

How can I change that?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by avazutaev · Mar 30, 2017 at 10:09 PM

If your space is in 2D (in 3D need some changes for this code) this should work:

 private Vector3 moveDirection;

in Update function:

 Vector3 currentPosition = transform.position;
 
 if( Input.GetButton("Fire1") ) {
   
   Vector3 moveToward = Camera.main.ScreenToWorldPoint( Input.mousePosition );
   
   moveDirection = moveToward - currentPosition;
   moveDirection.z = 0; 
   moveDirection.Normalize();
 }

  Vector3 target = moveDirection * moveSpeed + currentPosition;
     transform.position = Vector3.Lerp( currentPosition, target, Time.deltaTime );

P.S. If you want to run after mouse position without pushing it, just remove "if" statement

Comment
Add comment · Show 2 · 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 NFNProductions · Mar 30, 2017 at 10:22 PM 0
Share

Thanks, this actually would work as my Fire1 is by default mouse click 1. I actually could do that. But imagine that I want to hold down Jump (Space bar on my keyboard) to fly up or something whilst at the same time moving my mouse to move the player. In this scenario, when I hold down the space bar then Input.mousePosition does not update as the actual cursor on my screen does not move

avatar image avazutaev NFNProductions · Mar 31, 2017 at 09:16 AM 0
Share

you can use this script to move and put jump animation and play it by pushing the space button, or you can work in 3d than your moving script doesn't interrupt your jumping script.

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

98 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet to Mouse Position? 1 Answer

Projectile not firing from mouse location 1 Answer

Can't get a gameObject to follow my mouse 0 Answers

Raycast not drawing where i tell it 1 Answer

Shoot towards mouse with offset 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