• 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 Neoletum · Jan 11, 2018 at 08:14 PM · gameobjecttransformpositionmousepositiontile

[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int

Hey everyone! I'm creating a 2d top-down game and currently im working on a script which should keep the Gameobject position exactly at the same position of the mouse, but it doesn't work. Maybe it's because I'm casting a float to an int, but I have seen other games where it works very well.

Problem: When I move the mouse very fast the object glitches from it's position

I hope anyone can help me!

 void Update ()
     {
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         yMin = (int)playerRectAncor.transform.position.y - 1;
         yMax = (int)playerRectAncor.transform.position.y + 1;
         xMin = (int)playerRectAncor.transform.position.x - 1;
         xMax = (int)playerRectAncor.transform.position.x + 1;
 
         if (true) //Its just temporary true, for testing
         {
             pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             pos.z = transform.position.z;
             pos.x = Mathf.Clamp(pos.x, xMin, xMax);
             pos.y = Mathf.Clamp(pos.y, yMin, yMax);
            // put Object position to mousePosition
             rectObj.transform.position = new Vector3((int)pos.x, (int)pos.y, (int)pos.z);
         }
Comment
Add comment · Show 3
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 RocketFriday · Jan 11, 2018 at 10:36 PM 0
Share

Well, so you know, you can call update more than once a frame. But a better idea would be to smooth out the motion. Lastly, just guessing here, but maybe it is because you're casting to int. Floats give you the space in between values, ints will jerk from one value to another.

avatar image Orokon · Jan 13, 2018 at 01:38 AM 0
Share

Why are you even casting to int? The Vector3 constructor can take floats. Also the $$anonymous$$athf.Clamp takes floats: $$anonymous$$athf.Clamp

$$anonymous$$aybe the glitching comes from the int cast, 1.5 for example will get the 5 just cut off I guess when casting ins$$anonymous$$d of rounding.

So when you move the mouse across X/Y 1.0 -> X/Y 1.5 -> X/Y 2.1 it will move from X/Y 1 -> X/Y 1 -> X/Y 2

avatar image Bunny83 Orokon · Jan 13, 2018 at 02:16 AM 0
Share

Actually i don't get the whole point of all that code. Since he said the glitching only happens when he moves the mouse fast, that is of course related to the clamping. He clamps the new position to be in a rectangle (-1,-1,1,1) around the last position. So if the mouse moves further out the position gets clamped. This would cause the object to lag behind as each frame the position gets closer to the actual mouse position.


So i have no idea why he doesn't just do:

 rectObj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);

The int casting may be used to actually hit direct pixels. Though this would require an orthographic / 2d camera with a size equal to the screen height.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Neoletum · Jan 14, 2018 at 12:59 PM

Hey, thank you for your answer, it's difficult for me to describe my problem in english, maybe I described it bad, the point is that the game is tiled-base, and I'm casting it to an int because the whiteRect should only move in integers so the player can only build in a certain range around him.

In a nutshell, player should only be able to move the whiteRect in a 3x3 Range around his character otherwise he would be able to dig a hole out of his range^^ And moving the rect without casting it to an int seems not suitable to the kind of game im trying to create.

Here is a picture, it's like building in Stardew Valley:

alt text

@Bunny83 @RocketFriday @Orokon


problem2.png (121.0 kB)
Comment
Add comment · 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 Neoletum · Jan 14, 2018 at 02:48 PM 1
Share

So, I found the answer, the Problem was that a cast doesnt round up like I expected it. When the number becomes negativ it rounds in an other way, like this:

(int)2.5 = 2 (int)(-2.5) = -2 but it has to become -3!

Solution: Using $$anonymous$$athf.Floor(-2.5) rounds up to -3 and everything works fine.

Thank you all!

avatar image Orokon · Jan 14, 2018 at 03:14 PM 0
Share

Thats what I tried to tell you. You are not rounding but casting so it cuts off the numbers after the dot. 2.1 becomes 2, -2.1 becomes -2. With the usage of floor you will run into other Problems. Look at how $$anonymous$$athf.Floor behaves:

 Debug.Log($$anonymous$$athf.Floor(10.0F)); // returns 10
 Debug.Log($$anonymous$$athf.Floor(10.2F)); // returns 10
 Debug.Log($$anonymous$$athf.Floor(10.7F)); // returns 10
 Debug.Log($$anonymous$$athf.Floor(-10.0F)); // returns -10
 Debug.Log($$anonymous$$athf.Floor(-10.2F)); // returns -11
 Debug.Log($$anonymous$$athf.Floor(-10.7F)); // returns -11

If that is what you want thats great, otherwise you should look at the rounding functions.

Also you should use FloorToInt ins$$anonymous$$d of Floor so you dont have to cast, because it returns an int

avatar image Neoletum Orokon · Jan 14, 2018 at 10:24 PM 0
Share

Yes, but it works now like I expected it, thank you for your hints, when you answer not as a comment I can accept your answer =)

@Orokon

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

128 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 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

I want to move a cube with rotation but I find this problem 1 Answer

transform.position error 1 Answer

How to move a gameObject without affecting its childrens' positions? 2 Answers

How do you tranform multiple objects 1 Answer

Why is my variable updating when it shouldn't? 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