#LookRotation not looking at target in Non-Standard Gravity

1 messages · Page 1 of 1 (latest)

outer vault
#

I'm trying to implement a form of aim assist for my FPS and I'm currently working on getting the camera to snap to a target for testing.

My camera system is the one used in Catlike Coding's character controller tutorial meaning that the orientation is controlled by a local vector3 rotation with the X value controlling the pitch and the Y value controlling the Yaw. The vector3 variable is called orbitAngles for context.

So far, I can get the camera to snap to a target with the following line.

Vector3 AngleDifference = Quaternion.LookRotation((_currentTarget.position - transform.position),transform.up).eulerAngles;

float DebugAngleY = AngleDifference.y-orbitAngles.y;
float DebugAngleX = AngleDifference.x-orbitAngles.x;

//Testing. Force snap view to target
if (Input.GetKeyDown(KeyCode.Y))
{
  orbitAngles +=  new Vector3(DebugAngleX,DebugAngleY, 0);
  
  //Corrects the values so they are within angle restraints
  ConvertToDegrees();
}

This works when the player controller is using standard gravity directions however my game has gravity planes that change the gravity direction + player's orientation. If I try to snap to the target while on a gravity plane, the player will be looking in a different direction to the target. Sometimes it's close to it but never at the exact position of the target.

What can I do to get the camera to always look at the target object? Quaternions tend to do my head in so it could be something simples I'm overlooking.

#

FPS Camera: Snapping to target in Non-Standard Gravity

outer vault
#

LookRotation not looking at target in Non-Standard Gravity

runic bough
outer vault
icy berry
#

That's not going to work with Euler angles. In fact calculating the angle difference using Euler angles is probably the root of the problem in the first place

runic bough
#

Yep, I would probably use Vector3.SignedAngle to calculate the angle difference on both axes

#

And the clamping is an issue. For example, -90 can be reprsesented as 270, so that would actually get clamped to 90

#

I think it would be something like this```cs
var dir = _currentTarget.position - transform.position;
// Project the dir on each axis so we can calculate an accurate angle
var dirOnX = Vector3.ProjectOnPlane(dir, transform.right);
var dirOnY = Vector3.ProjectOnPlane(dir, transform.up);

// Calculate angles with the projected vectors
var xAngleDiff = Vector3.SignedAngle(transform.forward, dirOnX, transform.right);
var yAngleDiff = Vector3.SignedAngle(transform.forward, dirOnY, transform.up);```

#

Note that the x/yAngleDiff are the angles you need to add/rotate by to reach the desired direction

runic bough
#

Before clamping

outer vault
#

Bloody Hell, I was completely off. Greatly Appreciated, both of you. There is a slight issue with how the camera rotates if you are facing the opposite direction to the target but, I suspect this situation should never occur because the aim assist should only trigger if aiming at the target