I'm making basic item pickup like you would see in most first person games (Oblivion, Bioshock). Point the center of the camera to the item, click, and you have the item in your inventory. So I have a script attached to a camera (parented to an out of the box character controller) that sends out a raycast and then do some logic on the game object it hits.
//...
hit = new Raycast()
//...
void Update(){
if (Physics.Raycast(transform.position, transform.forward, out hit, 75f)){
//we hit something
lastOrigin = transform.position;
if(hit.transform != null){
lastPoint = hit.transform.position;
}
}
//if we want to interact with an object
if (Input.GetMouseButtonDown(0) || Input.GetButtonDown("Fire1")) //Left Click
{
//check if we're looking at any object
if ((hit.collider != null) && (hit.collider.gameObject != null) )
{
//OMITTED, ASSUMED UNIMPORTANT
}
}
//Draw where the last valid raycast was.
Debug.DrawLine(transform.position, lastPoint, Color.red);
}
Now, this works. To a point. I noticed after awhile my collisions were sort of flickering. So I ran a debug draw to see the raycast.
And got this. http://imgur.com/a/ukOaZ
If my screencaps aren't enough to explain it I noticed the origin point of the raycast was slowly sinking into the ground, but popping back to the place I wanted it to be. That means the transform position of the camera is drifting?! What?! Is raycast affected by Gravity by default? Is my Camera? Am I drawing the right thing? Has anyone else found this? Can anyone explain it?