- Home /
Question by
iamthecoolguy11 · Aug 19, 2013 at 06:20 AM ·
snappingblock
how do i get the to snap to the closest 1
I am placing a block and I need it to snap and I need to know if I can make this snap to the closest 1 instead of having a long decimal like 1.556955.
here is my script do not use my script without asking plz
var object : Transform;
function Update()
{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit) && Input.GetButtonUp("Place"))
{
Debug.DrawLine (ray.origin, hit.point);
Debug.Log(hit.point);
var xx=hit.point.x;
var yy=hit.point.y+0.55;
var zz=hit.point.z;
Instantiate (object, Vector3(xx, yy, zz), Quaternion.identity);
}
}
Comment
Best Answer
Answer by bubzy · Aug 19, 2013 at 06:44 AM
this is c# but I think you get the idea, please do not use my script without asking though. :)
using UnityEngine;
using System.Collections;
public class integerMagic : MonoBehaviour {
// Use this for initialization
float dude = 1.54f;
int hah = 0;
void Start () {
hah = (int)dude; //typecast the float as an integer. this will ignore everything after the decimal point, no rounding
}
// Update is called once per frame
void Update () {
Debug.Log(hah + "");
}
}
Answer by sotirosn · Oct 07, 2013 at 04:37 AM
Better to use Mathf.Round(), Mathf.Ceiling(), or Mathf.Floor() than to type cast in my opinion.
Your answer
Follow this Question
Related Questions
Rotations problem.. 1 Answer
Snapping Objects At Runtime 1 Answer
Hex Object Snapping 2 Answers
Scrollrect Snapping with Navigation 0 Answers
How can I change ProGrids scale of just the x or y axis? 0 Answers