I created this script to teleport where you look and every time I teleport to a horizontal object it makes me sink into the ground. I’m using this for moving with the google cardboard, but when I use a fps character it doesn’t sink please help.
private RaycastHit lastRaycastHit;
public AudioClip audioClip;
// Use this for initialization
void Start () {
Cursor.visible = false;
}
private GameObject GetLookedAtObject(){
Vector3 origin = transform.position;
Vector3 direction = Camera.main.transform.forward;
float range = 1000;
if (Physics.Raycast (origin, direction, out lastRaycastHit, range))
return lastRaycastHit.collider.gameObject;
else
return null;
}
private void TeleportToLookAt(){
transform.position = lastRaycastHit.point + lastRaycastHit.normal * 2;
if (audioClip != null)
AudioSource.PlayClipAtPoint (audioClip, transform.position);
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
if (GetLookedAtObject () != null)
TeleportToLookAt ();
}
}