コード例 #1
0
        private void FixedUpdate()
        {
            if (MoveToTarget != null)
            {
                /// TODO Change this into Path Finding behaviour.
                Vector3 target      = MoveToTarget.Vector3 + new Vector3(0.5F, 0, 0.5F);
                Vector3 direction   = (target - transform.position);
                float   sqrDistance = direction.sqrMagnitude;

                if (sqrDistance > 0.5F)
                {
                    direction.y = 0;
                    direction.Normalize();

                    float speed = m_running ? m_runSpeed : m_walkSpeed;

                    // get a normal for the surface that is being touched to move along it
                    RaycastHit hitInfo;
                    Physics.SphereCast(transform.position, m_characterController.radius, Vector3.down, out hitInfo,
                                       m_characterController.height / 2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
                    direction = Vector3.ProjectOnPlane(direction, hitInfo.normal).normalized;

                    m_moveDir.x = direction.x * speed;
                    m_moveDir.z = direction.z * speed;
                }
                else
                {
                    MoveToTarget = null;
                    m_moveDir    = Vector3.zero;
                }
            }

            if (m_characterController.isGrounded)
            {
                m_moveDir.y = -m_stickToGroundForce;
            }
            else
            {
                m_moveDir += Physics.gravity * m_gravityMultiplier * Time.fixedDeltaTime;
            }
            m_collisionFlags = m_characterController.Move(m_moveDir * Time.fixedDeltaTime);
        }
コード例 #2
0
 public Tile(TileMap owner, TilePosition position)
 {
     Owner           = owner;
     Position        = position;
     InstalledObject = null;
 }
コード例 #3
0
        public static TilePosition FromWorldPosition(Vector3 worldPos)
        {
            TilePosition position = new TilePosition(Mathf.FloorToInt(worldPos.x), Mathf.FloorToInt(worldPos.z));

            return(position);
        }
コード例 #4
0
 public void MoveTo(TilePosition target)
 {
     MoveToTarget = target;
 }
コード例 #5
0
        void Update()
        {
            float horizontal  = CrossPlatformInputManager.GetAxis("Mouse X");
            float vertical    = CrossPlatformInputManager.GetAxis("Mouse Y");
            float scrollWheel = CrossPlatformInputManager.GetAxis("Mouse ScrollWheel");

            if (Input.GetMouseButtonDown(0))
            {
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;

                if (Physics.Raycast(ray, out hitInfo))
                {
                    if (hitInfo.collider != null)
                    {
                        FocusTarget focusTarget = hitInfo.collider.GetComponent <FocusTarget>();
                        WorldController.Instance.MainState.Focus.On(focusTarget);
                    }
                }
            }

            if (Input.GetMouseButtonDown(1))
            {
                //Create a ray from the Mouse click position
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                //Initialise the enter variable
                float enter = 0.0f;

                if (m_groundPlane.Raycast(ray, out enter))
                {
                    Vector3      hitPoint     = ray.GetPoint(enter);
                    TilePosition tilePosition = TilePosition.FromWorldPosition(hitPoint);
                    Tile         targetTile   = WorldController.Instance.MainState.TileMap.TileAt(tilePosition);
                    if (targetTile != null)
                    {
                        if (!targetTile.HasObject)
                        {
                            Worker selectedWorker = WorldController.Instance.MainState.Focus.Current as Worker;
                            if (selectedWorker != null)
                            {
                                selectedWorker.MoveTo(tilePosition);
                            }
                        }

                        /*if(!targetTile.HasObject)
                         * {
                         *  targetTile.Install(new WallTileObject());
                         * }
                         * else
                         * {
                         *  targetTile.UninstallObject();
                         * }*/
                    }
                }
            }

            if (Input.GetMouseButton(2))
            {
                Vector3 forward = transform.forward;
                forward.y = 0;
                forward.Normalize();
                Vector3 right = transform.right;
                right.y = 0;
                right.Normalize();

                Vector3 position = transform.position;
                position          += -right * horizontal * m_moveSpeedFactor;
                position          += -forward * vertical * m_moveSpeedFactor;
                transform.position = position;

                m_focusTarget = null;
            }

            if (CrossPlatformInputManager.GetButtonDown("Turn Camera Left"))
            {
                RotateBy(m_cameraTurnIncrement);
            }

            if (CrossPlatformInputManager.GetButtonDown("Turn Camera Right"))
            {
                RotateBy(-m_cameraTurnIncrement);
            }

            if (scrollWheel != 0 || m_lastDistance != m_distance)
            {
                m_distance = Mathf.Clamp(m_distance - scrollWheel * m_scrollSpeed * (1 + m_distance * m_scrollAccellerationFactor), m_minDistance, m_maxDistance);
                UpdateCameraPosition();
                m_lastDistance = m_distance;
            }

            if (m_yRotationProgress < 1.0F)
            {
                m_yRotationProgress += Time.deltaTime * m_rotationSpeedFactor;
                if (m_yRotationProgress > 1.0F)
                {
                    m_yRotationProgress = 1.0F;
                }
            }

            transform.rotation = Quaternion.Euler(0, CalculateCurrentYOrientation(), 0);

            if (m_focusTarget != null)
            {
                Vector3 difference = m_focusTarget.transform.position - transform.position;
                float   t          = Mathf.Min(Mathf.Pow(m_focusFollowSpeed, Time.deltaTime * 100), 1);
                transform.position += difference * (t);
            }
        }