Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

428
Views
Unity - Added extra offset in facing target maybe difference between Rotate() vs transform.rotation?

I am confused to if using Rotate() and .rotation will conflict with each other.

I am moving a transform to point at a Target automatically with .rotation and when I choose to move manually I use Rotate(). But after I stop manual control and use the automatic mode the transform will be offset with the manual movement I had done. So the Rotate() is adding offset. The manual control no matter what is accurate. The automatic targeting is added an offset after using the manual control.

Here is the code for the Manual Control.

xDif = Input.GetAxis("Mouse X") * mouseSensitivity.x;
yDif = -Input.GetAxis("Mouse Y") * mouseSensitivity.y;
float zDif = 0; // <-- gets changed with buttons
transformToMove.Rotate(new Vector3(yDif, xDif, zDif));

Here is the code for Automatic Targeting

/// Returns true if the transform has been successfully oriented to fire at the current target.
private bool TrackTarget(out Vector3 hitPosition, out float hitTime)
{
    hitPosition = Vector3.zero;
    hitTime = -1.0f;
    
    if (!IsTargetValid(currentTarget))
    {
        state = TransformState.NoTarget;
        return false;
    }

    if (!GetFiringDirectionToHitTarget(currentTarget, out Vector3 firingDirection, out hitPosition, out hitTime))
    {
        return false;
    }
    
    Quaternion desiredRotation = Quaternion.LookRotation(invertFiringDirection ? -firingDirection : firingDirection);
    Quaternion currentRotation = transformToMove.rotation;
    float maxDeltaAngle = maxRotationDegreesPerSecond * Time.fixedDeltaTime;

transformToMove2.rotation = Quaternion.RotateTowards(
    currentRotation, 
    desiredRotation, 
    maxDeltaAngle
);
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

It sounds like your desiredRotation variable is being influenced by the current rotation of the player. Your desiredRotation is being set based on the LookRotation() of a firingDirection. Is your GetFiringDirectionToHitTarget() method using the player's current position, by chance?

over 4 years ago · Santiago Trujillo Report

0

Basically it was a “stupid” bug. In my manual control, I was applying the rotation to transformToMove and in the Editor it was applied to a child of the parent transform. So obviously there was going to be an offset if the initial rotation is for the parent (transformToMove2).

Sorry about this, I will leave the post up to teach Unity and even programmers in the future. That no matter how many times you look over your code and refactor it, the solution can be right in front of your eyes. For example, using the wrong transform.....

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!