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 MichaelARoberts · Jul 03, 2015 at 02:53 AM · gridmousepositionsnappinground

Sprite Snap and Follow Mouse

Been having problems getting this short script to have an object snap to round number (int). The object I attached it to continues to have a trailing decimal(float). I have a feeling it's because of "ScreenToWorldPoint". I'm using the 2D mode on unity, and my camera is orthographic. I want an effect similar to grid snapping.

Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class MoveSnap : MonoBehaviour {
 
     Vector3 mousePos = new Vector2(0,0);
     float mousePosX = 0;
     float mousePosY = 0;
 
     // Update is called once per frame
     void Update () {
         mousePosX = Input.mousePosition.x;
         mousePosY = Input.mousePosition.y;
         transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Mathf.Round(mousePosX),
                                                                         Mathf.Round(mousePosY),
                                                                         10.0f));
 
     }
 }
Comment
Add comment · Show 4
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 incorrect · Jul 03, 2015 at 03:02 AM 1
Share
 Vector2 mPosition = Input.mousePosition;
 
 Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(mPosition.x,
                                                                          mPosition.y,
                                                                          10.0f));
 
 transform.position = new Vector3($$anonymous$$athf.Round(point.x), $$anonymous$$athf.Round(point.y), $$anonymous$$athf.Round(point.z));
avatar image MichaelARoberts · Jul 03, 2015 at 03:37 AM 0
Share

So I need to store the ScreenToWorldPoint data before I assign it to my object. Thanks for the help!

avatar image incorrect · Jul 03, 2015 at 04:22 AM 0
Share

@$$anonymous$$ichaelARoberts, no, you don't. I don't assign ScreenToWorldPoint data to your object. Did not noticed?

avatar image MichaelARoberts · Jul 03, 2015 at 04:28 AM 0
Share

that's what I'm saying, you assign it so it's not used directly. You use the data from the variable from the "point" to set the new transform.position data so that it can round correctly.

I also noticed my "mousePos" was a Vector3 type not Vector2.

Thanks again.

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

An UI element will follow the mouse and snap to a grid, but why is the grid shifted after every game start? 0 Answers

UI scrollable rect, get position on content based on mousePos. 0 Answers

How To Sync Animation To Music or Bpm ? 2 Answers

Can Objects Snap Together In-Game Without A Grid? 1 Answer

Snapping an object to another 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