Esempio n. 1
0
    private IEnumerator RotateAroundEdge()
    {
        hasFinishedMoving = false;
        yield return(new WaitForSeconds(switchTime));

        Vector3 _newPos;
        MapLine _newMapLine, _nextMapLine;

        //rb.MovePosition (new Vector3 (transform.position.x, transform.position.y, 0));
        //rb.constraints = RigidbodyConstraints.FreezePositionZ;
        if (GameObject.Find("Player") != null)
        {
            _currPlayerNum = GameObject.Find("Player").GetComponent <PlayerShip> ().curMapLine.GetLineNum();
            if (_isCW == 0)
            {
                int _beCW  = _currPlayerNum - thisMapLine.GetLineNum();
                int _beCCW = _mapManager.mapLines.Length - _currPlayerNum + thisMapLine.GetLineNum();
                if (_beCW > _beCCW)
                {
                    _isCW = 1;
                }
                else if (_beCW < _beCCW)
                {
                    _isCW = -1;
                }
                else                     //Equal distance from player
                {
                    if (Random.value > 0.5)
                    {
                        _isCW = 1;
                    }
                    else
                    {
                        _isCW = -1;
                    }
                }
            }
            //Move (_isCW);

            /*
             *      thisMapLine.UpdateMovement (transform.position, Time.deltaTime * _isCW * movementForce * 0.2f, out _newPos, out _newMapLine);
             *      if (_newMapLine != null) {
             *              thisMapLine = _newMapLine;
             *      }
             */
            if (thisMapLine == GameObject.Find("Player").GetComponent <PlayerShip> ().curMapLine)
            {
                _nextMapLine = thisMapLine;
            }
            else if (_isCW == 1)
            {
                _nextMapLine = thisMapLine.leftLine;
                //Debug.Log ("Left");
            }
            else
            {
                _nextMapLine = thisMapLine.rightLine;
                //Debug.Log ("Right");
            }
            if (_nextMapLine != null)
            {
                thisMapLine = _nextMapLine;
            }
            _newPos = _nextMapLine.GetMidPoint();
            Debug.Log("Current Position: " + transform.position);
            Debug.Log("New Position: " + _newPos);
            transform.position = new Vector3(_newPos.x, _newPos.y, 0);
            //rb.MovePosition (new Vector3 (_newPos.x, _newPos.y, 0));
            Vector3 curDirVec = _nextMapLine.GetDirectionVector();
            Vector3 newDirVec = new Vector3(-curDirVec.y, curDirVec.x, 0);
            //print (Quaternion.Euler(newDirVec));
            transform.rotation = Quaternion.LookRotation(new Vector3(0f, 0f, 1f), newDirVec);
            //rb.MoveRotation (Quaternion.LookRotation (new Vector3 (0f, 0f, 1f), newDirVec));
            hasFinishedMoving = true;
        }
    }
Esempio n. 2
0
    // Update is called once per frame
    void Update()
    {
        if (rb.position.z <= 0)         //In case the player ship is flying in after respawning?
        {
            Vector3 _newPos;
            MapLine _newMapLine, _nextMapLine;
            //transform.position = new Vector3 (transform.position.x, transform.position.y, 0);
            rb.MovePosition(new Vector3(transform.position.x, transform.position.y, 0));
            rb.constraints = RigidbodyConstraints.FreezePositionZ;
            _currPlayerNum = GameObject.Find("Player").GetComponent <PlayerShip> ().curMapLine.GetLineNum();
            if (_isCW == 0)
            {
                int _beCW  = _currPlayerNum - thisMapLine.GetLineNum();
                int _beCCW = _mapManager.mapLines.Length - _currPlayerNum + thisMapLine.GetLineNum();
                if (_beCW > _beCCW)
                {
                    _isCW = 1;
                }
                else if (_beCW < _beCCW)
                {
                    _isCW = -1;
                }
                else                 //Equal distance from player
                {
                    if (Random.value > 0.5)
                    {
                        _isCW = 1;
                    }
                    else
                    {
                        _isCW = -1;
                    }
                }
            }
            //Move (_isCW);
            thisMapLine.UpdateMovement(transform.position, Time.deltaTime * _isCW * movementForce * 0.2f, out _newPos, out _newMapLine);
            rb.MovePosition(new Vector3(_newPos.x, _newPos.y, 0));
            if (_newMapLine != null)
            {
                thisMapLine = _newMapLine;
            }
            if (thisMapLine == GameObject.Find("Player").GetComponent <PlayerShip> ().curMapLine)
            {
                _nextMapLine = thisMapLine;
            }
            else if (_isCW == 1)
            {
                _nextMapLine = thisMapLine.leftLine;
            }
            else
            {
                _nextMapLine = thisMapLine.rightLine;
            }
            Vector3 curDirVec = _nextMapLine.GetDirectionVector();
            Vector3 newDirVec = new Vector3(-curDirVec.y, curDirVec.x, 0);
            //print (Quaternion.Euler(newDirVec));
            rb.MoveRotation(Quaternion.LookRotation(new Vector3(0f, 0f, 1f), newDirVec));
        }
        else if (_straightMovement)
        {
            //Only move in Z direction, aka depth
            //rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY;
            //rb.AddForce (-1 * movementForce * transform.forward * Time.deltaTime);

            rb.MovePosition(transform.position + transform.forward * (Time.deltaTime * movementForce * -1));
        }
        else         //Switching lanes
        {
            Vector3 _newPos;
            MapLine _newMapLine, _nextMapLine = thisMapLine.leftLine;
            Vector3 _newPosZ = transform.position + transform.forward * (Time.deltaTime * movementForce * -1);
            //Move forward by one or a few pixels
            thisMapLine.UpdateMovement(transform.position, Time.deltaTime * 1 * movementForce * 0.2f, out _newPos, out _newMapLine);
            rb.MovePosition(new Vector3(_newPos.x, _newPos.y, _newPosZ.z));
            //While moving to next section of map
            Vector3 curDirVec = _nextMapLine.GetDirectionVector();
            Vector3 newDirVec = new Vector3(-curDirVec.y, curDirVec.x, transform.position.z);
            rb.MoveRotation(Quaternion.LookRotation(new Vector3(0f, 0f, 1f), newDirVec));
        }
    }