Search Unity

Snapping in VR

Discussion in 'VR' started by sujeet007, Feb 27, 2019.

  1. sujeet007

    sujeet007

    Joined:
    Aug 30, 2018
    Posts:
    6
    I want to implement snapping of one game object to the other like in "Google Blocks" application. Snapping should happening irrespective of game object custom or predetermined mesh, transform.

    Right i'm able to snap perfectly to the snap able surface of game object if it is not rotated.

    In the reference below images you can check it out:
    Snap1: Before start of snap
    Snap2: Snapping perfecting when game object is not rotated and is irrespective of scale
    Snap3: Not snapping perfectly when the snapping game object is rotated
    Snap1.PNG Snap2.PNG Snap3.PNG
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    How do you want it to snap and what is your code at the moment?
     
  3. sujeet007

    sujeet007

    Joined:
    Aug 30, 2018
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RaycastNormal : MonoBehaviour
    6. {
    7.  
    8. public float RayDistance = 5;
    9.  
    10. Vector3[] directions = new Vector3[] { Vector3.forward, Vector3.up, Vector3.right, Vector3.back, Vector3.down, Vector3.left };
    11. RaycastHit raycastHit;
    12. private bool stopraycast;
    13. private Vector3 somevector;
    14. private Vector3 tempPos;
    15.  
    16. // Use this for initialization
    17. void Start()
    18. {
    19. stopraycast = true;
    20. somevector = Vector3.zero;
    21. }
    22.  
    23. // Update is called once per frame
    24. //void Update()
    25. //{
    26. // if (stopraycast)
    27. // DoRaycast();
    28. // stopraycast = false;
    29.  
    30. // if (somevector != Vector3.zero && stopraycast == false)
    31. // {
    32. // this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, somevector, 5f * Time.deltaTime);
    33. // Debug.Log(this.transform.localPosition + " " + somevector);
    34. // }
    35. //}
    36.  
    37. //void DoRaycast()
    38. //{
    39. // for (int i = 0; i < directions.Length; i++)
    40. // {
    41. // //if (Physics.Raycast(transform.position, transform.TransformDirection(directions), out raycastHit, RayDistance, 0 | 1))
    42. // if (Physics.Raycast(transform.position, directions, out raycastHit, RayDistance))
    43. // {
    44.  
    45. // Debug.DrawRay(transform.position, transform.TransformDirection(directions) * RayDistance, Color.blue);
    46.  
    47. // if (raycastHit.collider != null)
    48. // {
    49. // //this.transform.position = Vector3.MoveTowards(this.transform.localPosition, raycastHit.normal,5f * Time.deltaTime);
    50.  
    51.  
    52. // Debug.DrawRay(raycastHit.normal, Vector3.forward * 5, Color.green);
    53. // //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal);
    54. // this.transform.rotation = raycastHit.transform.localRotation;
    55. // somevector = raycastHit.normal;
    56. // Debug.Log(somevector);
    57. // //return raycastHit.normal;
    58. // }
    59. // }
    60. // }
    61. //}
    62.  
    63. // Update is called once per frame
    64. void Update()
    65. {
    66. //DoRaycast();
    67.  
    68. this.transform.position = Vector3.MoveTowards(this.transform.position, DoRaycast(), 0.5f * Time.deltaTime);
    69. }
    70.  
    71. private Vector3 DoRaycast()
    72. {
    73. for (int i = 0; i < directions.Length; i++)
    74. {
    75. //if (Physics.Raycast(transform.position, transform.TransformDirection(directions), out raycastHit, RayDistance, 0 | 1))
    76. if (Physics.Raycast(transform.position, directions, out raycastHit, RayDistance))
    77. {
    78. Debug.DrawRay(transform.position, transform.TransformDirection(directions) * RayDistance, Color.blue);
    79. //Debug.DrawRay(raycastHit.normal, -directions * 5, Color.green);
    80.  
    81. if (raycastHit.collider != null)
    82. {
    83.  
    84. if (i == 0)
    85. {
    86. //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal);
    87. this.transform.rotation = raycastHit.transform.localRotation;
    88. tempPos = raycastHit.transform.position;
    89. tempPos.z = raycastHit.transform.position.z - (raycastHit.transform.localScale.z + this.transform.localScale.z) / 2;
    90. Debug.Log(tempPos);
    91. }
    92. else if (i == 1)
    93. {
    94. //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal);
    95. this.transform.rotation = raycastHit.transform.localRotation;
    96. tempPos = raycastHit.transform.position;
    97. tempPos.y = raycastHit.transform.position.y - (raycastHit.transform.localScale.y + this.transform.localScale.y) / 2;
    98. Debug.Log(tempPos);
    99. }
    100. else if (i == 2)
    101. {
    102. //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal);
    103. this.transform.rotation = raycastHit.transform.localRotation;
    104. tempPos = raycastHit.transform.position;
    105. tempPos.x = raycastHit.transform.position.x - (raycastHit.transform.localScale.x + this.transform.localScale.x) / 2;
    106. Debug.Log(tempPos);
    107. }
    108. else if (i == 3)
    109. {
    110. //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal);
    111. this.transform.rotation = raycastHit.transform.localRotation;
    112. tempPos = raycastHit.transform.position;
    113. tempPos.z = raycastHit.transform.position.z + (raycastHit.transform.localScale.z + this.transform.localScale.z) / 2;// - this.transform.localScale.z);
    114. Debug.Log(tempPos);
    115. }
    116. else if (i == 4)
    117. {
    118. this.transform.rotation = raycastHit.transform.localRotation;
    119. tempPos = raycastHit.transform.localPosition;
    120. tempPos.y = raycastHit.transform.localPosition.y + (raycastHit.transform.localScale.y + this.transform.localScale.y) / 2;
    121. Debug.Log(tempPos);
    122.  
    123. //var distanceToGround = raycastHit.distance;
    124. //var currentPos = transform.localPosition;
    125. //var newY = currentPos.y - distanceToGround;
    126. //tempPos = new Vector3(currentPos.x, newY + (this.transform.localScale.y) / 2, currentPos.z);
    127. Debug.Log(tempPos);
    128.  
    129. }
    130. else if (i == 5)
    131. {
    132. //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal);
    133. this.transform.rotation = raycastHit.transform.localRotation;
    134. tempPos = raycastHit.transform.position;
    135. tempPos.x = raycastHit.transform.position.x + (raycastHit.transform.localScale.x + this.transform.localScale.x) / 2;
    136. Debug.Log(tempPos);
    137. }
    138.  
    139. }
    140. }
    141. }
    142.  
    143. return tempPos;
    144. }
    145. }
     
    Last edited: Feb 27, 2019
  4. sujeet007

    sujeet007

    Joined:
    Aug 30, 2018
    Posts:
    6
    should snap over the center of surface of gameobject, even if it is rotated like in 3rd image
     
    Last edited: Feb 27, 2019
  5. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Ok. Please read here on how to post code nicely in the forums - Using code tags properly

    Do you mean you want it to snap to the center of the face and is everything going to be cubes?
     
  6. sujeet007

    sujeet007

    Joined:
    Aug 30, 2018
    Posts:
    6
    ---snap center of the face - yes.
    ---everything cube - no(can be any custom mesh)
     
    Last edited: Feb 27, 2019