#Set Torso Up Rotation Based on Ray Hit

1 messages · Page 1 of 1 (latest)

chilly plover
#

Hey all, I'm working on a retro PSX 3rd person shooter, and emulating the over-the-shoulder style aiming we saw in later ganes such as RE4 or Gears of War.
I have a raycast shooting from camera and it detects hits in front of my character.
I need that hit point to feed my chest bone's Vector3.up rotation.
I have the camera pitch working great on my torso, but this needs extra math I'm unsure of how to do in Unity.
I'm assuming I can calculate the angle between my player forward vector and the chest bone to ray hit, then add that Quanternion to my local rotation.
I can write it on paper and it makes sense, but I'm not sure what Unity's C# package has to allow me to calculate this.

chilly plover
#

What AI is suggesting, and seems to make sense, is to get the angle by converting the point between my chest and aimpoint into a flat vector.
So Vector3. ProjectOnPlane(toTarget, Vector3.up).normalized

#

So when I get home I'm going to try to lay these values out step by step. Let me paste what I have. I could definitely use some feedback.

#
// player: the transform you consider "forward" for yaw (your player root or hips)
// aimPoint: world-space Vector3 hit point

Vector3 toTarget = aimPoint - chest.position;
Vector3 toTargetFlat = Vector3.ProjectOnPlane(toTarget, Vector3.up).normalized;
if (toTargetFlat.sqrMagnitude < 1e-6f) return; // target is almost on the chest

Vector3 playerForwardFlat = Vector3.ProjectOnPlane(player.forward, Vector3.up).normalized;
if (playerForwardFlat.sqrMagnitude < 1e-6f) playerForwardFlat = Vector3.forward;

float yawDegrees = Vector3.SignedAngle(playerForwardFlat, toTargetFlat, Vector3.up);
// yawDegrees is positive when target is to the right of playerForward, negative to the left
chilly plover
#

I actually figured this one out, and will update when I get home this week. I made some awesome progress that actually might help some folks.

placid dust
#

Since you have a direction in the first place, it would be easier to work with direction vectors, instead of angles, and in the end convert it to a rotation quaternion.

chilly plover