private bool IsOnGround() { float angle = Mathf.Atan2(_heading.x, _heading.z); foreach (Vector3 skinVertex in _groundSkinVertices) { float newXValue = Mathf.Cos(angle) * skinVertex.x - Mathf.Sin(angle) * skinVertex.z; float newZValue = Mathf.Cos(angle) * skinVertex.z + Mathf.Sin(angle) * skinVertex.x; Debug.DrawRay(_boxCollider.bounds.center + new Vector3(newXValue, skinVertex.y, newZValue), Vector3.up * 2f, Color.red, 1f); RaycastHit hit; if (Physics.Raycast(_boxCollider.bounds.center + new Vector3(newXValue, skinVertex.y, newZValue), Vector3.down, out hit, DistToGround, Layer)) { BlockFaceBehaviour hitBlockFace = hit.collider.GetComponent <BlockFaceBehaviour>(); if (!hitBlockFace.FireRaycastFromFace(0.1f, Layer, BlockFace.Top)) { return(true); } } } return(false); }
public void MoveBlock(BlockFaceBehaviour blockFaceBehaviour, BlockFace face) { _blockFace = blockFaceBehaviour; _face = face; _playerController.SetMobility(false); _playerController.SetGravity(false); _isMovingTowardsBlock = true; _blockFacePosition = blockFaceBehaviour.transform.position + face.GetNormal() * (0.5f + _playerController.GetBoxCollider().bounds.extents.z); }
// Use this for initialization private void Start() { _blockFaceBehaviour = GetComponent <BlockFaceBehaviour>(); _currentSpeed = InitialSpeed; foreach (BlockPlugin plugin in Plugins) { BlockPlugin pluginInstance = Instantiate(plugin); SubscribePlugin(pluginInstance); _plugins.Add(pluginInstance); } }
// Update is called once per frame private void Update() { if ((Mathf.Abs(Input.GetAxis("Horizontal")) != 0 || Mathf.Abs(Input.GetAxis("Vertical")) != 0) && _isMobile) { _animator.MovePlayer(); Move(); } else { _animator.StopPlayer(); } if (Input.GetAxis("Jump") == 1.0f && IsOnGround() && _isMobile) { Jump(); } // Checking for blocks the player is standing on foreach (BlockBehaviour block in _blockList) { block.SetIsPlayerStandingOn(false); } _blockList = new List <BlockBehaviour>(); for (int i = 0; i < 40; i++) { RaycastHit hit; Debug.DrawRay(_boxCollider.bounds.center + _groundSkinVertices[i], Vector3.down); if (Physics.Raycast(_boxCollider.bounds.center + _groundSkinVertices[i], Vector3.down, out hit)) { BlockBehaviour hitBlock = hit.collider.GetComponent <BlockBehaviour>(); BlockFaceBehaviour hitBlockFace = hit.collider.GetComponent <BlockFaceBehaviour>(); if (hitBlock != null && hit.normal == Vector3.up && !hitBlockFace.FireRaycastFromFace(0.1f, Layer, BlockFace.Top)) { hitBlock.SetIsPlayerStandingOn(true); if (!_blockList.Contains(hitBlock)) { _blockList.Add(hitBlock); } } } } }
void SetNewFaces() { foreach (Vector3 direction in _directions) { GameObject blockGameObject = GetGameObjInDir(direction); BlockFace blockFaceOfNeighbouringBlock = BlockFaceMethods.BlockFaceFromNormal(-direction); if (blockGameObject == null) { continue; } BlockFaceBehaviour blockFaceBehaviour = blockGameObject.GetComponent <BlockFaceBehaviour>(); BlockBehaviour blockBehaviour = blockGameObject.GetComponent <BlockBehaviour>(); if (blockBehaviour.IsTranslating()) { continue; } foreach (BlockPlugin plugin in blockBehaviour.GetPlugins()) { MoveablePlugin moveable = plugin as MoveablePlugin; if (moveable == null) { continue; } if (!moveable.IsDisplaced() || blockFaceOfNeighbouringBlock == moveable.GetDisplacedFace()) { BlockFace moveableFace = moveable.IsDisplaced() ? moveable.GetDisplacedFace() : blockFaceOfNeighbouringBlock; blockFaceBehaviour.HighlightFace(blockFaceOfNeighbouringBlock); _faceMap.Add(blockFaceBehaviour, moveableFace);; } break; } } }
private void HandleMouseClick() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; bool didRayCastHit = Physics.Raycast(ray, out hit); // https://answers.unity.com/questions/50279/check-if-layer-is-in-layermask.html // looking for block collisions only bool isHitTargetInCollidableLayer = didRayCastHit ? CollidableLayer == (CollidableLayer | (1 << hit.transform.gameObject.layer)) : false; if (didRayCastHit && isHitTargetInCollidableLayer) { BlockFaceBehaviour blockFace = hit.transform.gameObject.GetComponent <BlockFaceBehaviour>(); BlockFace face = BlockFaceMethods.BlockFaceFromNormal(hit.normal); if (_faceMap.ContainsKey(blockFace) && face == _faceMap[blockFace]) { _animator.MoveBlock(blockFace, face); } } }