qt configuration system new project function

2023-01-30   ES  

Unity click on the model at any position, the model rotates, so that the click position faces the camera
The effect is as follows

1. When the mouse clicks the screen, start from the camera to the mouse screen coordinates to obtain the rays and ballsPcoordinates (the ball needs to be added with SPHERE Collider collision box)
2. Build two vectors
vector AFrom the ball heartOpoint to the camera,a= camera coordinates -ball heart coordinates
vector BOpoint to the intersectionPb = P-Ball Heart Coordinates
3. How to calculate the rotation?
first look at the figure below
We need to take the vectorbAfter rotation and vectora

First need to confirm a rotation shaft
vectorbFork multiplication vectoraGet vectorcross
vectorcrossVertical in vectoraand vectorb
So vectorcrosscan be used as a vectorbRotating shaft

Calculation vectorbto vectoraangle angle angle, and then calculate the vectorcrossis the four yuan of the angle degree of the angle degree

Quaternion quaternion = Quaternion.AngleAxis(angle, cross);

Last order vectorc= four yuan quaternion left multiplication vectorb
vectorcJust follow the vectora
Vector3 c = quaternion * b;

The above is about the rotation of the vector

What we need to rotate the sphere, the same reason is the same
order four yuan result = four -yuan quaternion left rotating four yuan number

The result of
four -yuan number is the four dollars that the ball finally rotates

Quaternion result = quaternion * transform.rotation;

New RotationTo.cs script, mount the script to the object that needs to be rotated, run the unity, and click the model to see the effect. code show as below

using UnityEngine;

public class RotationTo : MonoBehaviour
    private Transform tr;
    private Quaternion finalQuaternion = Quaternion.identity;

    void Start()
        tr = transform;

        Transform redPoint = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
        redPoint.transform.localScale = Vector3.one * 0.2f;
        redPoint.GetComponent<MeshRenderer>().material.color = Color.red;

        Vector3 position = tr.position + (Camera.main.transform.position - tr.position).normalized * tr.localScale.x;
        redPoint.transform.position = position;

    void Update()

        if (Input.GetMouseButtonDown(0))

        // Use a smooth rotation of the difference function to the final orientation
        tr.rotation = Quaternion.Lerp(tr.rotation, finalQuaternion, 0.01f);

    // Get the coordinate of the sphere on the sphere
    private void GetTheClickPoint()
        // Example A ray from the main camera to the mouse click position
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        // Launch the rays. If the rays collide to the object with a color component, it will return True, otherwise it will return false
        if (Physics.Raycast(ray, out hitInfo))
            // hitinfo.point is the coordinates of rays and spherical collision points (mouse clicks on the spherical coordinates)
            finalQuaternion = CalculateRotation(hitInfo.point);

    // Calculate the rotation of the ball on the ball on the ball
    private Quaternion CalculateRotation(Vector3 hitPoint)
        // Get the vector of the ball center to click the ball surface
        Vector3 v1 = hitPoint - tr.position;

        // Verites to get the ball heart to the main camera
        Vector3 v2 = Camera.main.transform.position - tr.position;

        // The ball rotation is based on the vector (French vector) of the interface with V1 and V2 as a rotating axis through the ball heart and V1 and V2
        // V1 and V2 fork multiplication, obtain the French vector of the V1 and V2 composition interface
        Vector3 cross = Vector3.Cross(v1, v2);

        // The angle of the rotation of the ball rotating shaft is the angle of V1 and V2
        // Get vector V1 and V2 angle
        float angle = Vector3.Angle(v1, v2);

        // Get the ball with Cross as a rotating axis rotating angle angle.
        Quaternion quaternion = Quaternion.AngleAxis(angle, cross);

        // This error, you must have a oner.rotation
        //return tr.rotation * quaternion;
        return quaternion * tr.rotation;


Related Posts

ESD electrostatic protection diode silk print query, SOD-323 packaging model

Smartgit Ignore ignore the files that do not want to be managed by Git

JSOUP parsing the error of the file path of the XML report, perfect solution. Patriotic XML uses JSOUP1. Use JSOUP to complete the analysis of Student.xml, and give the parsed data to the Student object, and finally output the object on the console.

Spring-IOC container-Keke Teacher

qt configuration system new project function

Random Posts

vue2 data hijacking principle

Face the operation of a brand new cloud server

Right sidebar and return to the top right sidebar code SPONGEBOB

Chengxin University 807 Program Comprehensive Design 2015-2022 Real Questions and Answers [Continuous Update]

Repeated work: Be alert to the emotet virus email during the heavy insurance period