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 Ghachii · Apr 15, 2012 at 02:52 PM · javascriptgriddragsnap

Snap to grid after click and drag?

I'm using two scripts from the Unify Community wiki - one is to drag and drop objects, and the other snaps objects to a grid. Is there a way I can connect the variables of these scripts so that when you release the mouse, the object you've been dragging snaps to the grid?

DragObject script:

 var normalCollisionCount = 1;
 var moveLimit = .5;
 var collisionMoveFactor = .01;
 var addHeightWhenClicked = 0.0;
 var freezeRotationOnDrag = true;
 var cam : Camera;
 private var myRigidbody : Rigidbody;
 private var myTransform : Transform;
 private var canMove = false;
 private var yPos : float;
 private var gravitySetting : boolean;
 private var freezeRotationSetting : boolean;
 private var sqrMoveLimit : float;
 private var collisionCount = 0;
 private var camTransform : Transform;
 
 function Start () {
     myRigidbody = rigidbody;
     myTransform = transform;
     if (!cam) {
         cam = Camera.main;
     }
     if (!cam) {
         Debug.LogError("Can't find camera tagged MainCamera");
         return;
     }
     camTransform = cam.transform;
     sqrMoveLimit = moveLimit * moveLimit;   // Since we're using sqrMagnitude, which is faster than magnitude
 }
 
 function OnMouseDown () {
     canMove = true;
     myTransform.Translate(Vector3.up*addHeightWhenClicked);
     gravitySetting = myRigidbody.useGravity;
     freezeRotationSetting = myRigidbody.freezeRotation;
     myRigidbody.useGravity = false;
     myRigidbody.freezeRotation = freezeRotationOnDrag;
     yPos = myTransform.position.y;
 }
 
 function OnMouseUp () {
     canMove = false;
     myRigidbody.useGravity = gravitySetting;
     myRigidbody.freezeRotation = freezeRotationSetting;
     if (!myRigidbody.useGravity) {
         myTransform.position.y = yPos-addHeightWhenClicked;
     }
 }
 
 function OnCollisionEnter () {
     collisionCount++;
 }
 
 function OnCollisionExit () {
     collisionCount--;
 }
 
 function FixedUpdate () {
     if (!canMove) return;
    
     myRigidbody.velocity = Vector3.zero;
     myRigidbody.angularVelocity = Vector3.zero;
     myTransform.position.y = yPos;
     var mousePos = Input.mousePosition;
     var move = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, camTransform.position.y - myTransform.position.y)) - myTransform.position;
     move.y = 0.0;
     if (collisionCount > normalCollisionCount) {
         move = move.normalized*collisionMoveFactor;
     }
     else if (move.sqrMagnitude > sqrMoveLimit) {
         move = move.normalized*moveLimit;
     }
    
     myRigidbody.MovePosition(myRigidbody.position + move);
 }
 
 @script RequireComponent(Rigidbody)



SnapToGrid script:

 @MenuItem ("GameObject/Snap to Grid %g")
 static function MenuSnapToGrid()
 {
     var transforms : Transform[] = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
 
     var gridx : float = 1.0;
     var gridy : float = 1.0;
     var gridz : float = 1.0;
 
     for (var transform : Transform in transforms)
     {
         var newPosition : Vector3 = transform.position;
         newPosition.x = Mathf.Round(newPosition.x / gridx) * gridx;
         newPosition.y = Mathf.Round(newPosition.y / gridy) * gridy;
         newPosition.z = Mathf.Round(newPosition.z / gridz) * gridz;
         transform.position = newPosition;
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eugenius · Jan 14, 2013 at 05:08 PM

This is just a suggestion as I don't have Unity currently open. I am trying to achieve the same thing but haven't tried this yet.

What if you make use of your "canMove" boolean. Use that and (eventually) create one more variable (which is triggered as true whenever you press a button -- designated for use after you position the object wherever you want --) to create an:

//Check if the variables are false and true

if (!canMove && newBoolean) {

MenuSnapToGrid(); //Run the MenuSnapToGrid function -- set it as the last function or whatever

}

Try this and keep me posted as I'm very interested in the subject!

Hope this helps!

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
avatar image
0

Answer by tayloryork · Jan 31, 2013 at 05:33 AM

Change your 'OnMouseUp' function and add the gridx, gridy, and gridz variables to the script.

 private var snapToGridX : float = 1.0;
 private var snapToGridY : float = 1.0;
 private var snapToGridZ : float = 1.0;
     
 function OnMouseUp () {
     canMove = false;
     myRigidbody.useGravity = gravitySetting;
     myRigidbody.freezeRotation = freezeRotationSetting;
     if (!myRigidbody.useGravity) {
         myTransform.position.y = yPos-addHeightWhenClicked;
     }
     
     var newPosition : Vector3 = myTransform.position;
     newPosition.x = Mathf.Round(newPosition.x / gridx) * gridx;
     newPosition.y = Mathf.Round(newPosition.y / gridy) * gridy;
     newPosition.z = Mathf.Round(newPosition.z / gridz) * gridz;
     myTransform.position = newPosition;
         
 }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to drag gameobject (only x y) snapped to a grid? 2 Answers

Snapping To A Grid - Trouble With Mathf.Round 1 Answer

Javascript for simple grid puzzle? 1 Answer

Gameobject follow mouse on center 1 Answer

Snap object to grid with offset? 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