Calculate target angle based on current target angle and position?

I was struggling with what I think should be a very simple problem:

Diagram of the problem...

I know the current angle of the heading (say 15 degrees), and given a given gameObject transformation, I want to calculate the angle that I have to rotate to meet the target. In the above example, I want to calculate the 335ish deg value.

Note that I need to calculate the target angle, I have another class that, given the angle, will take care of the rotation of the title to the correct angle. I know Transform.LookAt () and other similar functions, they do not apply in this situation because the reasons (in another class I am basically calculating Euler Quaternion and Lerp until I reach the target angle due to design constraints, Transform.LookAt ( ) does not work).

In the first approach I tried to calculate angle theta with Dot product Vector3.forward and direction vectors, didn't work quite right (turned forever or in wrong direction)

Diagram of the problem

targetAngle = Vector3.Angle(Vector3.forward, (target pos - current pos).normalized);

      

I thought maybe if , I could calculate the current direction vector of the course, I could take the Dot product of this and the direction vector of the target to get the theta angle, then use theta and the current angle for the shape (360 is theta or whatever) then?) to get the angle I want to rotate. The fact is that I only have the current angle and the current pos, I don't understand how to calculate the current heading direction vector from this information. Am I adding some arbitrary constant to the current position of the Z position and subtracting from the current position to get the vector dir? Seems hacky and like it shouldn't work.

Diagram of the problem

Any ideas are appreciated.

Edit: More information

The orientation code / u / Asad asked about:

    // Calculate the target Quat to rotate all children by
    Quaternion targ = Quaternion.Euler(0, 0, targetAngleHeading);
    // Calculate the Linear interpolation to apply to all children
    Quaternion lerp = Quaternion.Lerp(children[0].transform.localRotation, targ, Time.deltaTime * speedHeading_dps);

    foreach (GameObject c in children)
    {
        // Apply lerp calcualted above to all children
        c.transform.localRotation = lerp;
    }
    // Update the current heading angle 1st child local z angle
    currentAngleHeading = turrets[0].transform.localEulerAngles.z;

      

Then in FixedUpdate () I have:

    if (currentAngleHeading != targetAngleHeading)
    {
        DesiredHeading();
    }

      

+3


source to share


2 answers


You can cast your transform forward and vector to your target and get an angle or rotation from them:

Vector3 vectorToTarget = target.transform.position - transform.position;
Vector3 facingDirection = transform.forward; // just for clarity!

float angleInDegrees = Vector3.Angle(facingDirection, vectorToTarget);
Quaternion rotation = Quaternion.FromToRotation(facingDirection, vectorToTarget);

      



Note that the rotation is relative; those. this is the rotation that needs to be applied from the current position so that it faces the target.

+1


source


try this instead

you can directly take the distance vector from transform.position and then vector3.normalize (target.position - object.position)



this is the corner object you want to move

0


source







All Articles