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 /
avatar image
0
Question by Kmausser · Mar 25, 2017 at 08:52 AM · c#vrvector3quaternionsnap

Object snapping to floor and other objects in VR

I want to snap objects to the floor, then lock them in place so the player can snap other objects neatly adjacent to the other objects without having to fiddle with rotating it just right. I was originally going to use VRTK snapping, but I want somewhat of a freeform room furniture placement system without having to place thousands of “snap drop zones” to account for the possibilities. Is there a snapping code that works in VR?

I looked around and found this code from a YT tut: https://youtu.be/n2fx9rkMMVU { Vector3 gridSnapPos(Vector3 originalPosition) { float granularity = .1f; Vector3 snappedPosition = new Vector3(Mathf.Floor(originalPosition.x / granularity) granularity, originalPosition.y, Mathf.Floor(originalPosition.z / granularity) granularity); return snappedPosition; }

public void Snap() { this.transform.position = gridSnapPos(this.transform.position);

} } The problem I have now is that it jitters and jumps too much when grabbing it, so I made it enable on untouch and disable after running 1 frame: { void OnEnable() { Snap(); enabled = false; } } and whenever it’s enabled now, sitting on the floor completely still, it will jump in the -x direction.

I want it to stay put where I place it, but still allow other objects to collide with it so I can place them neatly adjacent. (edited)

Also, I tried doing rotation snapping using the same code just replacing “vector” with “quaternion” and “position” with “rotation”, doesn’t really work.

I’m trying to use it in conjunction with Fuseman’s Force tutorial: https://youtu.be/uf4sz7YbF04 ForceGrab.cs : which goes on the controllers { using System.Collections; using System.Collections.Generic; using UnityEngine; using VRTK;

public class ForceGrab : MonoBehaviour {

 private SteamVR_Controller.Device controller;
 private LineRenderer lRender;
 private Vector3[] positions;
 private ForceGrabItem grabbable;

 private Snapping snapIt;

 private bool grabbed;

 private Vector3 lastHandPos;
 private Quaternion lastHandRot;

 private VRTK_InteractableObject interactable;

 // Use this for initialization
 void Start () {
     SteamVR_TrackedObject trackedObj = GetComponent<SteamVR_TrackedObject>();
     controller = SteamVR_Controller.Input((int) trackedObj.index);
     lRender = GetComponent<LineRenderer>();
     positions = new Vector3[2];
     
 }
 
 // Update is called once per frame
 void Update () {
     if (!grabbed)
     {
         grabbable = RaycastForGrabbableObject();
         if (!grabbable) return;
     }

     Vector3 currHandPos = transform.position;
     Quaternion curHandRot = transform.rotation;
     if (controller.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
     { // force grab
         grabbed = true;
         grabbable.Grab(true);
         grabbable.SetMoveScale(transform.position);
         lastHandPos = currHandPos;
         lastHandRot = curHandRot;
         DisplayLine(false, transform.position);
     }
     else if (controller.GetPress(SteamVR_Controller.ButtonMask.Trigger))
     { // force move
         grabbable.Move(currHandPos, lastHandPos, curHandRot, lastHandRot);
     }
     else if (controller.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
     { // release
         grabbed = false;
         grabbable.Grab(false);
         snapIt.Snap();
         //GetComponent snapping enable disable
     
     }
     lastHandPos = currHandPos;
     lastHandRot = curHandRot;
 }

 private ForceGrabItem RaycastForGrabbableObject()
 {
     RaycastHit hit;
     Ray r = new Ray(transform.position, transform.forward);

    // Debug.DrawRay(transform.position, transform.forward);

     if (Physics.Raycast(r, out hit, Mathf.Infinity) && hit.collider.gameObject.GetComponent<ForceGrabItem>() != null)
     {
         if (hit.collider.gameObject.GetComponent<VRTK_InteractableObject>())
         {
             interactable = hit.collider.gameObject.GetComponent<VRTK_InteractableObject>();
             interactable.ToggleHighlight(true);
             interactable.StartTouching(gameObject);
         } 

         DisplayLine(true, hit.point);
         return hit.collider.gameObject.GetComponent<ForceGrabItem>();
        // print("hit");
     }
     else
     {
         if (interactable != null)
         {
             interactable.ToggleHighlight(false);
             interactable.StopTouching(gameObject);
             interactable = null;
         }
         
         DisplayLine(false, transform.position);
         return null;
         // print("not hit");
     }
        

 }

 void DisplayLine(bool display, Vector3 endpoint)
 {
     lRender.enabled = display;
         positions[0] = transform.position;
         positions[1] = endpoint;
         lRender.SetPositions(positions);
 }

} } ForceGrabItem.cs : which goes on the interactable object { using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ForceGrabItem : MonoBehaviour {

 private Rigidbody rBody;
 private float moveScale;

 // Use this for initialization
 void Start () {
     rBody = GetComponent<Rigidbody>();
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 public void Grab(bool shouldGrab)
 {
     rBody.isKinematic = shouldGrab;
 }
 
 public void SetMoveScale(Vector3 handPosition)
 {
     Vector3 origin = GameObject.FindGameObjectWithTag("MainCamera").transform.position;
     moveScale = Vector3.Magnitude(transform.position - origin) / Vector3.Magnitude(handPosition - origin);
 }

 public void Move(Vector3 curHandPos, Vector3 lastHandPos, Quaternion curHandRot, Quaternion lastHandRot)
 {
     rBody.MovePosition(rBody.position + (curHandPos - lastHandPos) * moveScale);
     rBody.MoveRotation(Quaternion.RotateTowards(lastHandRot, curHandRot, Time.deltaTime));
 }

} } And Snapping.cs : which goes on the interactable object { using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Snapping : MonoBehaviour { void OnEnable() { Snap(); enabled = false; } //Position snap Vector3 gridSnapPos(Vector3 originalPosition) { float granularity = .1f; Vector3 snappedPosition = new Vector3(Mathf.Floor(originalPosition.x / granularity) granularity, originalPosition.y, Mathf.Floor(originalPosition.z / granularity) granularity); return snappedPosition; }

//Rotatation snap (is broken, makes object freak out) /Quaternion gridSnapRot(Quaternion originalRotation) { float granularity = .1f; Quaternion snappedRotation = Quaternion.Euler(Mathf.Floor(originalRotation.x / granularity) granularity, originalRotation.y, Mathf.Floor(originalRotation.z / granularity) granularity); return snappedRotation; }/

 public void Snap()
 {
     this.transform.position = gridSnapPos(this.transform.position);
     

     //this.transform.rotation = gridSnapRot(this.transform.rotation);

     Debug.LogError("snap");
 }

   

} } This is what it currently looks like: https://youtu.be/yGpmjgf2AYs

Help?

Comment
Add comment · Show 1
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 kavanavak · May 20, 2017 at 04:22 AM 0
Share

I'm also looking for a snapping solution, have you found a solid one? If not, i'll forward whatever i find.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by kavanavak · May 20, 2017 at 08:46 AM

I'm also looking for a snapping solution, have you found a solid one? If not, i'll forward whatever i find.

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 Kmausser · May 20, 2017 at 10:10 AM 0
Share

I haven't worked on it in a while and I haven't found any answers. This seems like a basic common thing but apparently it isn't.

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

7 People are following this question.

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

Related Questions

calculating looking angle between 2 transforms 0 Answers

Rotate player (rigidbody) towards his movement 2 Answers

Pose.ctor - create pose from vector3 and quaternion 1 Answer

Slerp look at direction not working... 3 Answers

Saving and applying an array of Vector3 velocities on an array of GameObjects 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