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 /
This question was closed Jan 16, 2016 at 04:30 PM by LockBros for the following reason:

Answer found elsewhere

avatar image
0
Question by LockBros · Jan 14, 2016 at 08:03 PM · mouse

Make an object move in place of the mouse X and Y [UPDATE]

I am working on a game where you can build in blocks. I want the player to be able to see the outline of the block they are about to place down before they place it so that they can see what it will look like.

To do this I am wanting to replace the mouse with the block or have a block follow the mouse's X and Y so that it is always in the same position as the mouse and not lagging behind.

I have looked for a solution but the block is never fixed to the mouse or the block floats up to the camera and on the example I found I could not work out how to stop it doing so,

Thanks in advance.

EDIT:

I have go the block to follow the mouse perfectly with the following script:

 Vector3
         screenPoint,
         offset,
         scanPos,
         curPositionx,
         curPositiony,
         curScreenPoint;
     
     
     public float
         gridSize = 1;
     
     
     
     void Awake() {
         scanPos = gameObject.transform.position;
         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
         offset = scanPos - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         
     }
     
     
     
     
     void Update() {
         curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         curPositionx = Camera.main.ScreenToWorldPoint (curScreenPoint);
         curPositiony = Camera.main.ScreenToWorldPoint (curScreenPoint);
         
         
         curPositionx.x = (float)(Mathf.Round (curPositionx.x) / gridSize);
         curPositiony.y = (float)(Mathf.Round (curPositiony.y) / gridSize);
     
         
         transform.position = curPositionx;
         transform.position = curPositiony;
         
     }
     
     
 }


However it glides Smoothly on the grid and does not snap to the grid.

If you remove the " curPositiony " and the lines of code that are with it:

  curPositiony = Camera.main.ScreenToWorldPoint (curScreenPoint);
 
  curPositiony.y = (float)(Mathf.Round (curPositiony.y) / gridSize);
 
  transform.position = curPositiony;


Then the X axis will snap but the Y axis still wont.

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

  • Sort: 
avatar image
0

Answer by toddisarockstar · Jan 14, 2016 at 08:12 PM

i did something like this with a past project. is the game three dimentional? do the the blocks "snap" to a position or do you need smooth movement on X and Y? if it is 3D, do the blocks stack? your answer is going to come from "raycasting" from camera to mouse position. but with a couple answers i can give code example.

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 LockBros · Jan 14, 2016 at 08:15 PM 0
Share

Hi @toddisarockstar the game is 3D and I am wanting it to be able to snap on a grid. I have a grid but I felt that the mouse needed a visual box before i tried to do snapping as I would not be able to see if it worked or not.

Follow this Question

Answers Answers and Comments

38 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

Related Questions

Why is my MouseLook script not letting me look up? 0 Answers

Convert game to Android 2 Answers

CATCH MOUSE CLICK EVENT ON OBJECTS 0 Answers

How to use a bluetooth mouse in Gear VR? 1 Answer

getting maximum x and y values of mouse position 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