Ejemplo n.º 1
0
        void LateUpdate()
        {
            if (env == null || !env.applicationIsPlaying || !crosshairEnabled)
            {
                return;
            }

            UpdateCrosshairScreenPosition();

            Ray          ray = m_Camera.ScreenPointToRay(input.screenPos);
            VoxelHitInfo hitInfo;

            // Check if there's a voxel in range
            crosshairOnBlock = env.RayCast(ray, out hitInfo) && hitInfo.voxelIndex >= 0;
            if (!input.GetButton(InputButtonNames.Button1) || crosshairHitInfo.GetVoxelNow().isEmpty)
            {
                crosshairHitInfo = hitInfo;
            }
            if (crosshairOnBlock)
            {
                crosshairOnBlock = FastVector.SqrDistance(ref crosshairHitInfo.voxelCenter, ref curPos) < crosshairMaxDistance * crosshairMaxDistance;
                if (!crosshairOnBlock)
                {
                    crosshairHitInfo.Clear();
                }
            }
            if (changeOnBlock)
            {
                if (!crosshairOnBlock)
                {
                    ResetCrosshairPosition();
                    return;
                }
                // Puts crosshair over the voxel but do it only if crosshair won't disappear because of the angle or it's switching from orbit to free mode (or viceversa)
                float d = Vector3.Dot(ray.direction, crosshairHitInfo.normal);
                if (d < -0.2f)
                {
                    crosshair.position = hitInfo.point;
                    crosshair.LookAt(hitInfo.point + crosshairHitInfo.normal);
                }
                else
                {
                    crosshair.localRotation = Misc.quaternionZero;
                }
                crosshairMat.color = crosshairOnTargetColor;
            }
            if (crosshairOnBlock)
            {
                crosshair.localScale = Misc.vector3one * (crosshairScale * (1f - targetAnimationScale * 0.5f + Mathf.PingPong(Time.time * targetAnimationSpeed, targetAnimationScale)));
                env.VoxelHighlight(crosshairHitInfo, voxelHighlightColor, voxelHighlightEdge);
            }
        }
Ejemplo n.º 2
0
        void LateUpdate()
        {
            Collider collider = null;

            if (env != null && env.characterController != null)
            {
                // Check object on the crosshair
                collider = env.characterController.crosshairHitInfo.collider;
                if (env.input.GetButtonDown(InputButtonNames.Action))
                {
                    if (collider != null)
                    {
                        VoxelPlayInteractiveObject obj = collider.GetComponentInChildren <VoxelPlayInteractiveObject> ();
                        if (obj != null)
                        {
                            if (obj.triggerNearbyObjects)
                            {
                                for (int k = 0; k < nearCount; k++)
                                {
                                    obj = nearObjs [k];
                                    if (obj != null && obj.playerIsNear)
                                    {
                                        nearObjs [k].OnPlayerAction();
                                    }
                                }
                            }
                            else if (obj.playerIsNear)
                            {
                                obj.OnPlayerAction();
                            }
                        }
                    }
                }
            }

            // Check nearby objects

            // Get player position
            Vector3 playerPos = env.currentAnchorPos;

            // Check if player has moved since last frame
            int playerPosX = (int)playerPos.x;
            int playerPosY = (int)playerPos.y;
            int playerPosZ = (int)playerPos.z;

            if (playerPosX == lastPlayerPosX && playerPosY == lastPlayerPosY && playerPosZ == lastPlayerPosZ && collider == lastCollider)
            {
                return;
            }
            lastCollider   = collider;
            lastPlayerPosX = playerPosX;
            lastPlayerPosY = playerPosY;
            lastPlayerPosZ = playerPosZ;

            // Check if player enters/exits the interaction area per object
            for (int k = 0; k < count; k++)
            {
                VoxelPlayInteractiveObject o = objs [k];
                if (o != null && o.enabled)
                {
                    Vector3 objPos = o.transform.position;
                    float   interactionDistanceSqr = o.interactionDistance * o.interactionDistance;
                    float   dist   = FastVector.SqrDistance(ref playerPos, ref objPos);
                    bool    isNear = dist <= interactionDistanceSqr;
                    if (o.playerIsNear && !isNear)
                    {
                        o.playerIsNear = false;
                        // Remove from near list
                        nearObjs [o.nearIndex] = null;
                        if (o.nearIndex == nearCount - 1)
                        {
                            nearCount--;
                        }
                        o.nearIndex = 0;
                        // Call event
                        o.OnPlayerGoesAway();
                    }
                    else if (isNear && !o.playerIsNear)
                    {
                        o.playerIsNear = true;
                        o.nearIndex    = AddToDynamicList(o, ref nearObjs, ref nearCount);
                        // Call event
                        o.OnPlayerApproach();
                    }
                }
            }
        }