LookAt with an offset

First off, use code tags:

oldRotation = transform.rotation;
transform.LookAt(hand.hoverSphereTransform.position);
diffRotation = transform.rotation.eulerAngles - oldRotation.eulerAngles;
transform.eulerAngles = transform.eulerAngles - diffRotation;

And

transform.LookAt(hand.hoverSphereTransform.position);
transform.eulerAngles = transform.eulerAngles - diffRotation;

Next, yes, don’t use euler angles. They’ll have issues like gimbal lock.

THOUGH, I don’t know what you’re wanting to do here. In the first one you get the look rotation, then get the delta/diff from current to the new look at. And then you set the angle to the lookat - diff?

That’s like saying:

result = target - (target - current);

Note that ‘target’ cancels out, and you’re left with current. Effectively no rotation.

In the second one you set your rotation to the delta/diff between target and current. Effectively:

result = target - current

Neither of these sound like:

So plugging your code in like so:

public class zTest01 : MonoBehaviour
{

    public Transform targ;

    private Vector3 diffRotation;

    private void Start()
    {
        var oldRotation = transform.rotation;
        transform.LookAt(targ.position);
        diffRotation = transform.rotation.eulerAngles - oldRotation.eulerAngles;
        transform.eulerAngles = transform.eulerAngles - diffRotation;
    }

    private void Update()
    {


        transform.LookAt(targ.position);
        transform.eulerAngles = transform.eulerAngles - diffRotation;
    }

}

I get a general idea of what you’re expecting.

It appears you want it where the angle at which you’re looking at an object on the pull of the trigger is to remain the angle off of that object as it moves.

So first off, lets realize that everything during start is unnecessary. You’re just calculating the ‘diffRotation’, everything else just sets it back to its current rotation.

And lets also do this with quats.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class zTest01 : MonoBehaviour
{

    public Transform targ;

    private Quaternion _delta;

    private void Start()
    {
        var lookAt = Quaternion.LookRotation(targ.position - this.transform.position);
        //this is how you calculate the amount of rotation from a->b. It's inv(a) * b.
        //note I'm going from lookAt to rotation, because we want to 'remove' it.
        _delta = Quaternion.Inverse(lookAt) * this.transform.rotation;
    }

    private void Update()
    {
        transform.rotation = Quaternion.LookRotation(targ.position - this.transform.position) * _delta;
    }

}

Or back in your variable names:

//on trigger pulled
var rot = Quaternion.LookRotation(hand.hoverSphereTransform.position - this.transform.position);
diffRotation = Quaternion.Inverse(rot) * this.transform.rotation;

//on trigger held
this.transform.rotation = Quaternion.LookRotation(hand.hoverSphereTransform.position - this.transform.position) * diffRotation;
3 Likes