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 sandwichmenno · Jun 22, 2015 at 03:42 PM · mousegridfollowsnap

Gameobject follow mouse on center

What needs to happend

The object is instantiated at the mouse position, so that the mouse is centered on the object. Then you can move the object over the terrain while snapping to the grid staying at the Y axis of 1.

What is happening

The cursor isn't calibrated right. It is positioned above the object, not on the center of the object. This causes a really weird placement of the object.

The red circle is where the mouse is positioned right now. The yellow circle is where it should be. (And the cube is the objects that is spawned in, but that is quite obvious I presume) example

The code Spawning the object:

 public void Spawn(string b) {
         var pos = Input.mousePosition;
         pos.z = -Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint(pos);
 
         path = "Objects/" + b + "/" + currentObject;
         Debug.Log ("Load path is " + path);
         GameObject spawnCurrentObject = Resources.Load(path) as GameObject;
         spawnObject = (GameObject)Instantiate(spawnCurrentObject, new Vector3(Mathf.Round (pos.x / gridSize) * gridSize, 50, Mathf.Round (pos.z / gridSize) * gridSize), Quaternion.identity);
     }


Moving the object:

 void Update () {    
         var pos = Input.mousePosition;
         pos.z = -Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint(pos);
 
         transform.position = new Vector3(Mathf.Round (pos.x / gridSize) * gridSize, 1, Mathf.Round (pos.z / gridSize) * gridSize);
     }
untitled-3.png (11.6 kB)
Comment
Add comment · Show 2
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 maccabbe · Jun 22, 2015 at 04:38 PM 0
Share

You probably just want to use Camera.ScreenPointToRay and Plane.Raycast, i.e.

 void Update () {  
     Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
     Plane plane=new Plane(Vector3.up, new Vector3(0, 1, 0));
     float distance;
     if(plane.Raycast(ray, out distance)) {
          transform.position = ray.GetPoint(distance);
     }
 }

avatar image sandwichmenno · Jun 22, 2015 at 08:57 PM 0
Share

Thank you maccabbe. It's quite odd how I didn't think of using this. Anyhow, I editted your code to my likings (with grid snapping). I will post the full solution and mark is as an answer.

1 Reply

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

Answer by sandwichmenno · Jun 22, 2015 at 09:09 PM

Credits to maccabbe!!!

 float gridSize = 2.0f;
     Vector3 points;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {    
         Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
         Plane plane=new Plane(Vector3.up, new Vector3(0, 1, 0));
         float distance;
         if(plane.Raycast(ray, out distance)) {
             points = ray.GetPoint(distance);
         }
 
         transform.position = new Vector3(Mathf.Round (points.x / gridSize) * gridSize, 1, Mathf.Round (points.z / gridSize) * gridSize);
     }
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

2 People are following this question.

avatar image avatar image

Related Questions

Accurate Mouse Snap to Grid within Threshold? (Float Rounding Errors) 1 Answer

Camera follow instantiated object and reset 0 Answers

Snap blocks that aren't cubes. 1 Answer

detect mouseover with grid buttons? 1 Answer

Raycast instantiating object to grid 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