示例#1
0
    // FixedUpdate is called one per specific time
    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");                                     //Get if Any Horizontal Keys pressed
        float moveVertical   = Input.GetAxis("Vertical");                                       //Get if Any Vertical Keys pressed

        GData bipolarMidpoint = gs.getBipolarMidpoint();

        if (bipolarMidpoint != null && bipolarMidpoint.getIntensityInGauss() > 0)
        {
            moveHorizontal = (bipolarMidpoint.getX() - 0.5f) * 2;
            moveVertical   = -(bipolarMidpoint.getY() - 0.5f) * 2;

            float angle = -bipolarMidpoint.getAngle() * Mathf.Rad2Deg;
            this.transform.rotation = Quaternion.Euler(0, 0, angle);
        }

        Vector2 movement = new Vector2(moveHorizontal, moveVertical);                   //Put them in a Vector2 Variable (x,y)

        GetComponent <Rigidbody2D>().velocity = movement * speed;                       //Add Velocity to the player ship rigidbody

        //Lock the position in the screen by putting a boundaries
        GetComponent <Rigidbody2D>().position = new Vector2
                                                (
            Mathf.Clamp(GetComponent <Rigidbody2D>().position.x, boundary.xMin, boundary.xMax),                      //X
            Mathf.Clamp(GetComponent <Rigidbody2D>().position.y, boundary.yMin, boundary.yMax)                       //Y
                                                );
    }