Unity—quaternion, Euler angle API+coordinate system

Quote of the day: There is no deadline for doing what you love

Make up for these two days~

Table of contents

Exercise: Calculate the coordinates of the object 30 degrees in front of the right and 10m away

Exercise: Calculate the coordinates of the two tangent points from the explosion point to the player

//Projection test ProjectOnPlane

Move the current object (necessary process) (Euler angle)

Quaternion API

Exercise: Rotate the character according to the direction entered by the user, and move forward

coordinate system

Unity coordinate system

World Space

Local Space

Screen Space

Viewport Space

Coordinate system conversion

Local Space—>World Space

World Space—>Local Space

World Space<—>Local Space

World Space<—>Viewport Space

Exercise: If you move out of the screen, stop the movement (can return); move out of the upper boundary of the screen, return to the lower boundary, move out of the left boundary, and return to the right boundary


Exercise: Calculate the coordinates of the object 30 degrees in front of the right and 10m away

public Vector3 vect;

    void Update()

    {

        Debug.DrawLine(this.transform.position, vect);

        vect = this.transform.position + Quaternion.Euler(0, 30, 0) *this.transform.rotation * new Vector3(0, 0, 10);

}

this .transform.position + move the vector to the current object position

* this .transform.rotation * new  Vector3(0, 0, 10) The vector rotates with the rotation of the current object

Exercise: Calculate the coordinates of the two tangent points from the explosion point to the player

 

Known: player radius Radius, player, bomb position

Ideas: 1. Calculate the direction from the player to the explosion point, and the radius long vector

           2. Rotate the vector according to the angle

The script gives the bomb, and the location of the bomb itself is known

(It doesn't matter how to find two objects, use tags to find them)

 

 

 

public string playerTag = "Player";

    private Transform playerTF;

    private float radius;

    void Start()

    {

        GameObject playerGO = GameObject.FindWithTag(playerTag);//获取玩家的游戏对象

        playerTF = playerGO.transform;

        radius = playerGO.GetComponent<CapsuleCollider>().radius;

    }

    private Vector3 leftTangent,rightTangent;

    void Update()

    {

        Vector3 playerToExplosion = this.transform.position - playerTF.position;

        Vector3 playerToExplosionDirection = playerToExplosion.normalized * radius;

        float angle = Mathf.Acos(radius / playerToExplosion.magnitude) * Mathf.Rad2Deg;

        leftTangent = Quaternion.Euler(0, -angle, 0) * playerToExplosionDirection + playerTF.position;

        rightTangent = Quaternion.Euler(0, angle, 0) * playerToExplosionDirection + playerTF.position;

        Debug.DrawLine(this.transform.position, leftTangent, Color.red);

        Debug.DrawLine(this.transform.position, rightTangent, Color.red);

}

Show results:

 

 sqrMagnitude The square of the modulus length If you only compare the lengths of two vectors, use sqrMagnitude, which is less than the square root of magnitude, saving performance

Vector3.OrthoNormalize(ref Vector3 normal,ref Vector3 tangent,ref Vector3 binormal)

Standardize the vector, return tangent, binormal and normal are vertical relationships

//Projection test ProjectOnPlane

 

public Transform t1;

 public Vector3 planeNorm;

 private void Update()

    {

        Vector3 result = Vector3.ProjectOnPlane(t1.position, planeNorm);

        Debug.DrawLine(Vector3.zero, result, Color.red);

        Debug.DrawLine(Vector3.zero, t1.position);

}

Move the current object (necessary process) (Euler angle)

//Move at a constant speed to reach the target point

this.transform.position= Vector3.MoveTowards (this.transform.position,new Vector3(0,0,10),0.1f);(starting point, end point, speed)

//Fast first and then slow, unable to reach the target point (infinite approach)//End point, fixed ratio

this.transform.position=Vector3.Lerp(this.transform.position,new Vector3(0,0,10),0.1f);

//natural movement

public AnimationCurve curve;//Animation curve

private float x;

public float duration=3;//duration (3 seconds to reach the end)

private void Update()

{ x+=Time.deltaTime/duration;//Accumulate the time consumed by each frame

  this.transform.position=Vector3.Lerp(Vector3zero,

new Vector3(0,0,10),curve.Evaluate(x));

Quaternion API

Quaternion -> Euler angle

Quaternion qt=this.transform.rotation;

Vector3 euler=qt.eulerAngles;

Axis/Angle Rotation

Quaternion Quaternion.AngleAxis(float angle, Vector3 axis); (angle, axis)

this.transform.rotation=Quaternion.AngleAxis(50,Vector3.up);

(equivalent to Quaternion.Euler(0,50,0);)

// gaze rotation (z axis)

public Transform tf;//The current object looks at tf rotation

Vector3 dir=tf.position-this.transform.position;//The current object looks at tf rotation

this.transform.rotation=Quaternion.LookRotation(dir);//Can be divided into multiple frames

(this.transform.LookAt(tf);)//One frame changes

//Difference rotation Lerp, uniform rotation Quaternion.RotateTowards

Quaternion dir=Quaternion.LookRotation(tf.position-this.transform.position);

this.transform.rotation=Quaternion.Lerp(this.transform.rotation,dir,0.1f);

Quaternion dir = Quaternion.Euler(0, 90, 0);

    void Update()

    {

        this.transform.rotation = Quaternion.Lerp(this.transform.rotation, dir, 0.1f);

        //如果当前旋转角度,接近目标旋转角度

        if(Quaternion.Angle(this.transform.rotation,dir)<1)

        {

            this.transform.rotation = dir;

        }

    }

//x-axis gaze rotation

this.transform.right=tf.position-this.transform.position;

Want to turn slowly—>Collect Lerp—>Quaternion

Quaternion dir=Quaternion.FormToRotation

(Vector3.right,tf.position-this.transform.position);

Exercise: Rotate the character according to the direction entered by the user, and move forward

public float moveSpeed = 3;

    public float rotateSpeed = 2;

    private void Update()

    {

        float hor = Input.GetAxis("Horizontal");

        float ver = Input.GetAxis("Vertical");

        Quaternion dir = Quaternion.LookRotation(new Vector3(hor, 0, ver));

        this.transform.rotation = dir;

        this.transform.rotation = Quaternion.Lerp(this.transform.rotation, dir, Time.deltaTime * rotateSpeed);

        if(hor!=0||ver!=0)

            this.transform.Translate(0, 0, Time.deltaTime * moveSpeed);

    }

coordinate system

Unity coordinate system

World Space

World (global) coordinate system: fixed coordinates for the entire scene

Role: Indicate the position and direction of each game object in the game scene

Local Space

Object (local) coordinate system: an independent coordinate system for each object, the origin is the pivot point of the model, which changes as the object moves or rotates

Function: Indicates the relative position and direction between objects

Screen Space

Screen coordinate system: in pixels, the bottom left corner of the screen is the origin (0,0), the screen width and height of the top right corner, z is the distance to the camera

Function: Indicates the position of the object on the screen

Viewport Space

Viewport (camera) coordinate system: the origin at the upper left of the screen is (0,0), the upper right corner is (1,1), and z is the distance to the camera

Role: Indicates the position of the object in the camera

Coordinate system conversion

Local Space—>World Space

transform.forward indicates that the object is in front of the world coordinate system

transform.right indicates that the object is on the right in the world coordinate system

transform.up indicates that the object is directly above in the world coordinate system

transform.TransformPoint transform point , affected by transform component position, rotation and scale

transform.TransformDirection direction , affected by the rotation of the transform component

transform.TransformPoint vector , affected by transformation component rotation and scaling

World Space—>Local Space

transform.InverseTransformPoint; transform point, affected by transform component position, rotation and scale

transform.InverseTransformDirection; direction, affected by the rotation of the transformation component

transform.InverseTransformVector; transformation point, affected by the rotation and scaling of the transformation component

World Space<—>Local Space

Camera.main.WorldToScreenPoint converts the point from the world coordinate system to the screen coordinate system

Camera.main.ScreenToWorldPoint converts the point from the screen coordinate system to the world coordinate system

World Space<—>Viewport Space

Camera.main.WorldToViewportPoint converts the point from the world coordinate system to the viewport coordinate system

Camera.main.ViewportToWorldPiont converts the point from the screen coordinate system to the world coordinate system

Exercise: If you move out of the screen, stop the movement (can return); move out of the upper boundary of the screen, return to the lower boundary, move out of the left boundary, and return to the right boundary

public float moveSpeed = 3;

    private Camera mainCamera;

       private void Start()

    {

        mainCamera = Camera.main;

    }

    private void Update()

    {



        float hor = Input.GetAxis("Horizontal");

        float ver = Input.GetAxis("Vertical");

        hor *= moveSpeed * Time.deltaTime;

        ver*= moveSpeed * Time.deltaTime;

        Vector3 screenPoint = mainCamera.WorldToScreenPoint(this.transform.position);

        //如果超过屏幕,停止运动

        //如果到了最左边 并且还想向左运动 或者到了最右边并且还想向右运动

        if (screenPoint.x <= 0 && hor < 0 || screenPoint.x >= Screen.width && hor > 0)

            hor = 0;

        //上出,下进

        if (screenPoint.y > Screen.height)

            screenPoint.y = 0;

        //下出,上进

        if (screenPoint.y < 0)

            screenPoint.y = Screen.height;

        this.transform.position = Camera.main.ScreenToWorldPoint(screenPoint);

        this.transform.Translate(hor, 0, ver);

    }

Guess you like

Origin blog.csdn.net/m0_63330263/article/details/126099261