Aim in same direction as camera (Look-Away)

Ok, I want to reverse engineer a look-at code to be a look-away code. In order to fix a problem with my previous aiming mechanism*** I decided to make the shooter part rotate in the same direction as my camera (rather than have my camera be the point that it’s attached to, like an FPS). Is there a way to make a look-at code: (something like this)

var cameraToLookAt: Camera;
 
function Start()
{
      cameraToLookAt = Camera.main;
}
function Update()
{
      var v: Vector3 = cameraToLookAt.transform.position - transform.position;
      v.x = v.z = 0.0;
      transform.LookAt(cameraToLookAt.transform.position - v);
}

Into a look-away code? (Note, that code only aims in one rotation axis, I would need all three for it, but I used it as an example). Basically I will be looking in the SAME direction as my camera. not the complete opposite.

***My orignal planning relates to FPS logic, where the camera is always at the character’s eyes. However, because I can zoom my camera in and out, it messes up where I shoot from. I just want the angle.

Can someone please give the code a look-over? I’m sure it’s as simple as switching a sign or something… If there’s a look-at there must be a look-away.

What you’re doing there seems kind of roundabout. I’d probably just write (pseudocode):

Vector3 p = target.position;
p.y = 0f;
transform.LookAt(p);

As for looking in the opposite direction, try:

transform.LookAt(transform.position - (p - transform.position));
1 Like

transform.localRotation = Quaternion.Inverse(Quaternion.LookRotation (look, Vector3.up)) * transform.rotation

this sort of thing gives the inverse of a rotation. In this case, it is the inverse of a look at camera rotation scheme. So it finds the reverse… so the object will face the same direction as the camera and stuffs 8^)

Just had to use a little of this in something I was doing.