- Home /
Raycast point to provive a target not working?
hello. I want to create away for an object to look at a 3d point in space, which my mouse hovers over. i've done this by projecting a raycast from my mouse to my 3d world, and using the '.point' api as the target Vector3 on the look at script. how come this ain't working?
i also want a graphic to be displayed off of the normals of the object my mouse hits, and i tried this with a particle emitter but it still doesn't work.
here's the code :
static var previousHit : RaycastHit; private var previousColor : Color; private var bObjectSelected = false; var marker : GameObject;
private var firstInit : boolean = true;
function Update ()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit, Mathf.Infinity))
{
previousHit = hit;
if(firstInit)
{
Instantiate(marker, hit.point, Quaternion.identity);
firstInit = false;
}
if(hit.collider.gameObject.tag == "Selectable" && Input.GetButton("Mouse1"))
{
previousColor = hit.collider.gameObject.renderer.material.color;
hit.collider.gameObject.renderer.material.color = Color.green;
bObjectSelected = true;
previousHit = hit;
print("Hit selectable");
}
}
}
function LateUpdate() { marker.transform.position = previousHit.point; }
it works when first instantiating the marker, the marker spawns under the mouse, but when i move the mouse, the marker doesn't follow?
Answer by Nathan Bennett · Jan 18, 2011 at 03:28 PM
I found that out that, creating a Vector3 Variable, and assigning it to the PayCastHit.point every frame, worked.