Y value wont work?

#pragma strict
var Firefrom : Transform;
var currentbrick: GameObject;

function Update () {
if(Input.GetButtonDown("Fire1")){
var hit : RaycastHit;
var roundpos : Vector3;



if(Physics.Raycast(Firefrom.position, Firefrom.forward, hit)){
//roundpos = 
Debug.Log(hit);
roundpos = hit.point;
roundpos *= 2.0;
roundpos = Vector3(Mathf.Round(roundpos.x), Mathf.Round(roundpos.y), Mathf.Round(roundpos.z));
roundpos /= 2.0;

Instantiate(currentbrick, roundpos , Quaternion.LookRotation(Vector3.zero));
}
}
}

This is my code to spawn a block (Soon to be things like wood or something)
it works all fine, exept that Y values will not work, and my question is, does raycast ignore cloned(Instantiated) objects? Because only way to make one on another is to click a brick’s top. So is raycast ignoring the cloned object? this is all confusing

On line 19, put Debug.Log(roundpos) to verify the position is the one you expect. Verify the collider is aligned with the brick in the prefab you are using for 'currentbrick.'

found out what problem was, Every few times, I would click to build, but the cube would actualy end up inside the other. my thoughs is that the raycast goes a little inside the cube, so it rounds it to there. Im not sure how to fix this. Maybe make the hit point actual .01 closer to the raycast start? if so, how could I do that?

what does those last two do?

1 Answer

1

This will move back along the Firefrom vector from hit.point by 0.01:

var pt = hit.point - Firefrom.forward.normalized * 0.01;