// Called each update to move sideways void Move() { if (legacyMovement == true) { Vector3 newPos; MapLine newMapLine; Quaternion newQuat; curMapLine.UpdateMovement(transform.position, Time.deltaTime * _inputValue * moveSpeed, out newPos, out newMapLine); if (movingForward == true) { newPos = newPos + new Vector3(0f, 0f, transform.position.z + moveSpeed * 0.02f); } _rigidbody.MovePosition(newPos); if (newMapLine != null) { curMapLine = newMapLine; } } else { Vector3 newPos = _nextMapLine.GetMidPoint(); if (movingForward == true) { newPos = newPos + new Vector3(0f, 0f, transform.position.z + moveSpeed * 0.02f); } _rigidbody.MovePosition(newPos); Vector3 curDirVec = _nextMapLine.GetDirectionVector(); Vector3 newDirVec = new Vector3(-curDirVec.y, curDirVec.x, 0); //print (Quaternion.Euler(newDirVec)); _rigidbody.MoveRotation(Quaternion.LookRotation(new Vector3(0f, 0f, 1f), newDirVec)); curMapLine = _nextMapLine; } }
// 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)); } }