- Home /
Question by
billyjoetyler · Feb 14, 2015 at 05:24 PM ·
raycastsnap
Lock to grid while raycasting
Hi, I have a question, is there a way to lock to a grid while raycasting. Here is how I raycast:
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
function Update(){
if(Physics.Raycast (ray, hit, Mathf.Infinity, myLayerMask)){
W_Wall_Model.transform.position = Vector3.Lerp(W_Wall.transform.position, hit.point, 50f);
}
}
I am aware that this only moves an existing object, This is what I use to snap to grid:
#pragma strict
function Start () {
}
function Update () {
var currentPos = transform.position;
transform.position = Vector3(Mathf.Round(currentPos.x),
Mathf.Round(currentPos.y),
Mathf.Round(currentPos.z));
}
But I have not found a way to raycast and snap the raycasted object to a grid. Does anybody have any ideas?
Comment
Best Answer
Answer by Tuncer · Feb 19, 2015 at 12:58 PM
Try this :
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
var realPosition: Vector3;
function Start()
{
realPosition = W_Wall_Model.transform.position;
}
function Update()
{
if(Physics.Raycast (ray, hit, Mathf.Infinity, myLayerMask))
{
realPosition = Vector3.Lerp(realPosition , hit.point, 50f);
W_Wall_Model.transform.position = Vector3(Mathf.Round(realPosition.x),Mathf.Round(realPosition.y),Mathf.Round(realPosition.z));
}
}
Your answer
Follow this Question
Related Questions
rotation problems when snapping to terrain 1 Answer
Raycast Snapping Problem. 0 Answers
Raycast instantiating object to grid 0 Answers
Snap objects together (Ingame) 0 Answers