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 Kyle_Katarn · Apr 03, 2014 at 02:00 PM · mouseswipe

Mouse Position to Screen Coordinate Problems

Hi all, I'm wondering if you could help me with a small issue I'm having.

I'm developing a sailing game for a project in College. It's going to be a game for the Android, eventually, but for now, I'm just developing it with PC controls for convenience. Trying to simulate a touch and swipe with the mouse is my current issue.

Basically, my boat's going to have broadside cannons, but the camera is fixed meaning that the boat's rotation is independent from the camera, so the direction of the swipe will determine which side the cannons are fired out of. That part is figured out though, I have a good idea on what I'm doing for that. My current problem is the actual inputs themselves.

When I click my mouse, it's in a fixed location, which could skew the direction result, I want it to update with my sailboat's movement, I also want to draw an arrow from the clicked position (in screen coordinates), to where the mouse currently is (in screen coordinates), but it never appears on screen, in fact, it appears way above the camera.

Does this make sense? I have a feeling I'm not getting my exact problems across.

My code is as follows (in the update function):

 // Get if the mouse is clicked and held
 if ( Input.GetMouseButtonDown( 0 ) )
 {
     _mouseClickPosition = Input.mousePosition + gameObject.transform.position;
 
           
     _isDragging = true;
 }
 
 // if you want to input an aiming arrow, do it here
 if ( _isDragging )
 {
     Debug.DrawLine(_mouseClickPosition, Input.mousePosition, Color.white);
 }
              
 
 // mouse released
 if ( Input.GetMouseButtonUp( 0 ) && _isDragging )
 {
     _mouseReleasePosition = Input.mousePosition;
     
 
     _isDragging = false;
 }

Edit

I should probably mention, what I'm aiming for is something like Angry Birds' slingshot mechanic (though it only depends on the rotation which isn't limited), except it'll be moving along with the player.

Edit 2

I utilized my fantastic paint skills to try and get a screenshot of my current problem

alt text

Comment
Add comment · Show 1
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 Kyle_Katarn · Apr 04, 2014 at 06:16 AM 0
Share

$$anonymous$$ain problem is figured out, I'll mark as answered and post a new question regarding updating the original click position.

2 Replies

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

Answer by Adamcbrz · Apr 03, 2014 at 03:43 PM

The problem with the Debug.DrawLine is because you are telling it what camera it should map it from. If you are using only the mainCamera you can call something like:

 Vector3 mousePositionInWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);

But that is still going to come from the camera. More than likely you want a collision layer below your ships. A large box collider then you can raycast from the camera to that point to figure out where it hit in your game world. Something like:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 if(Physics.Raycast(ray, 1000, out hit, layerMaskOfItemsYouWantToRaycastAgainst))
 {
       Vector3 groundPointWhereWeHit = hit.point;
 }
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 Kyle_Katarn · Apr 03, 2014 at 06:31 PM 0
Share

Hi, and thanks for replying! Unfortunately, I don't think that rays are what I'm looking for.

I currently have it as you initially suggested, using ScreenToWorldPoint, but due to the fact that my player is always moving (no plans so far for an "Anchor" feature :P), it means that the player is always moving away from the initial click point (_mouseClickPosition). I'm trying to figure out a solution to have that initial point move along with me wherever I go, but nothing seems to work.

Thanks again for the suggestions. :)

avatar image
0

Answer by Kyle_Katarn · Apr 03, 2014 at 03:44 PM

Okay, I got it to mostly work with using ScreenToWorldPoint (I had tried it previously, but it seems the line was being rendered too high for me to actually see).

Here's the updated code:

 // Get if the mouse is clicked and held
 if (Input.GetMouseButtonDown(0))
 {
     _mouseClickPosition = Camera.main.ScreenToWorldPoint( new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20));
 
     _isDragging = true;
 }
 
 // if you want to input an aiming arrow, do it here
 if (_isDragging)
 {
     Debug.DrawLine(
         _mouseClickPosition,
         Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.y, 20 ) ),
         Color.white );
 
 }
 
 
 // mouse released
 if (Input.GetMouseButtonUp(0) && _isDragging)
 {
     _mouseReleasePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20));
 
     _isDragging = false;
 }

There's still the problem of the origin point not moving with the player, so the beginning point of the line crawls off screen. Is there a way for it to keep up with the player? (The camera is above the player, looking down at a slight angle, while the player moves along the X and Z axis', if I add the gameObject.transform.position to the _mouseClickPosition, it'll have some strange behaviour of starting the line at different places).

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

swiping with mouse input 0 Answers

Detect mouse swipe on 2d object 1 Answer

destroy object by using line renderer 1 Answer

Mouse Swipe? 1 Answer

Using mouse up and down for mouse swipe 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