Placement by Raycast

Placement by Raycast

This is a sample of placing 3D objects on to the surface of buildings with the point of contact. Using mouse or touch panel, ray is casted that connects to camera's focal point, and thus calculate code based on transactional points with building mesh.

MaxstSceneManager.cs
public void AttachLogo()
{
    Vector2 vTouchPos = Input.mousePosition;

    Ray ray = Camera.main.ScreenPointToRay(vTouchPos);

    RaycastHit vHit;
    if (Physics.Raycast(ray.origin, ray.direction, out vHit))
    {
        maxstLogObject.transform.position = vHit.point;
        maxstLogObject.transform.rotation = Quaternion.FromToRotation(Vector3.forward, vHit.normal) * Quaternion.Euler(-90.0f ,0.0f, 0.0f);
    }
}

Last updated