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
Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class RaycastNormal : MonoBehaviour { public float RayDistance = 5; Vector3[] directions = new Vector3[] { Vector3.forward, Vector3.up, Vector3.right, Vector3.back, Vector3.down, Vector3.left }; RaycastHit raycastHit; private bool stopraycast; private Vector3 somevector; private Vector3 tempPos; // Use this for initialization void Start() { stopraycast = true; somevector = Vector3.zero; } // Update is called once per frame //void Update() //{ // if (stopraycast) // DoRaycast(); // stopraycast = false; // if (somevector != Vector3.zero && stopraycast == false) // { // this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, somevector, 5f * Time.deltaTime); // Debug.Log(this.transform.localPosition + " " + somevector); // } //} //void DoRaycast() //{ // for (int i = 0; i < directions.Length; i++) // { // //if (Physics.Raycast(transform.position, transform.TransformDirection(directions), out raycastHit, RayDistance, 0 | 1)) // if (Physics.Raycast(transform.position, directions, out raycastHit, RayDistance)) // { // Debug.DrawRay(transform.position, transform.TransformDirection(directions) * RayDistance, Color.blue); // if (raycastHit.collider != null) // { // //this.transform.position = Vector3.MoveTowards(this.transform.localPosition, raycastHit.normal,5f * Time.deltaTime); // Debug.DrawRay(raycastHit.normal, Vector3.forward * 5, Color.green); // //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal); // this.transform.rotation = raycastHit.transform.localRotation; // somevector = raycastHit.normal; // Debug.Log(somevector); // //return raycastHit.normal; // } // } // } //} // Update is called once per frame void Update() { //DoRaycast(); this.transform.position = Vector3.MoveTowards(this.transform.position, DoRaycast(), 0.5f * Time.deltaTime); } private Vector3 DoRaycast() { for (int i = 0; i < directions.Length; i++) { //if (Physics.Raycast(transform.position, transform.TransformDirection(directions), out raycastHit, RayDistance, 0 | 1)) if (Physics.Raycast(transform.position, directions, out raycastHit, RayDistance)) { Debug.DrawRay(transform.position, transform.TransformDirection(directions) * RayDistance, Color.blue); //Debug.DrawRay(raycastHit.normal, -directions * 5, Color.green); if (raycastHit.collider != null) { if (i == 0) { //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal); this.transform.rotation = raycastHit.transform.localRotation; tempPos = raycastHit.transform.position; tempPos.z = raycastHit.transform.position.z - (raycastHit.transform.localScale.z + this.transform.localScale.z) / 2; Debug.Log(tempPos); } else if (i == 1) { //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal); this.transform.rotation = raycastHit.transform.localRotation; tempPos = raycastHit.transform.position; tempPos.y = raycastHit.transform.position.y - (raycastHit.transform.localScale.y + this.transform.localScale.y) / 2; Debug.Log(tempPos); } else if (i == 2) { //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal); this.transform.rotation = raycastHit.transform.localRotation; tempPos = raycastHit.transform.position; tempPos.x = raycastHit.transform.position.x - (raycastHit.transform.localScale.x + this.transform.localScale.x) / 2; Debug.Log(tempPos); } else if (i == 3) { //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal); this.transform.rotation = raycastHit.transform.localRotation; tempPos = raycastHit.transform.position; tempPos.z = raycastHit.transform.position.z + (raycastHit.transform.localScale.z + this.transform.localScale.z) / 2;// - this.transform.localScale.z); Debug.Log(tempPos); } else if (i == 4) { this.transform.rotation = raycastHit.transform.localRotation; tempPos = raycastHit.transform.localPosition; tempPos.y = raycastHit.transform.localPosition.y + (raycastHit.transform.localScale.y + this.transform.localScale.y) / 2; Debug.Log(tempPos); //var distanceToGround = raycastHit.distance; //var currentPos = transform.localPosition; //var newY = currentPos.y - distanceToGround; //tempPos = new Vector3(currentPos.x, newY + (this.transform.localScale.y) / 2, currentPos.z); Debug.Log(tempPos); } else if (i == 5) { //this.transform.rotation = Quaternion.FromToRotation(-1 * directions, raycastHit.normal); this.transform.rotation = raycastHit.transform.localRotation; tempPos = raycastHit.transform.position; tempPos.x = raycastHit.transform.position.x + (raycastHit.transform.localScale.x + this.transform.localScale.x) / 2; Debug.Log(tempPos); } } } } return tempPos; } }
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?