0

I'm trying to set up a game with portals in it. I've been struggling for a while now to rotate the Player. I've tried Quat.Slerp, Quat.AngleAxis, and Quat.Euler. But through rigorous testing I discovered that the MouseLook function in my FPS Controller script is the reason the Player isn't rotating. The player is being rotated, but half a frame later, the MouseLook script resets the Player's rotation.
How can I rotate the player without letting the MouseLook function of my FPS Controller script get in the way?

void MouseLook()
{   mouseX = Input.GetAxisRaw("Mouse X") * sensX * Time.deltaTime;
    mouseY = Input.GetAxisRaw("Mouse Y") * sensY * Time.deltaTime;
yRot += mouseX;    xRot -= mouseY;
xRot = Mathf.Clamp(xRot, -50f, 50f);
transform.rotation = Quaternion.Euler(xRot, yRot, 0);

}

Jason Burley
  • 15
  • 1
  • 5

2 Answers2

0

These lines

yRot += mouseX;
xRot -= mouseY;

Are saying "Take the previous rotation values from mouse movement and add the latest mouse movement to them"

Which is then applied by this line:

transform.rotation = Quaternion.Euler(xRot, yRot, 0);

The easiest option would be to rotate the player by changing xRot and yRot, so the next time MouseLook() is called, it's taking your manual rotation into account.

Failing that, you could either read the current rotation (might become complex if the player is ever non-vertical) or having another variable to track manual rotation separately from mouse rotation and sum the two together when setting transform.rotation

Basic
  • 1,267
  • 9
  • 25
  • Can you show how you would take a manual rotation and convert it to the corresponding xRot and yRot values to set? – DMGregory Oct 17 '22 at 11:09
  • @DMGregory It's a simple addition, but you're welcome to edit it into my answer if you think it's needed. – Basic Oct 17 '22 at 11:27
  • if you already have an angle delta, sure. What if you have a target orientation as a quaternion, or a target facing vector, and need to distill that down to these two angle values? That's a little more than just addition, which is why I think it would be worth demoing explicitly. – DMGregory Oct 17 '22 at 12:22
  • @Basic I'm attempting to manually rotate the player in a separate script. So, suppose I make xRot and yRot public, I'd be able to change their values from my other script? Better yet, I think I'll try performing the rotation within my FPS controller script...brb (EDIT) K im back. Its not gonna work coz im doing math from a separate script. Unless I can access one script from another??? – Jason Burley Oct 17 '22 at 17:38
  • You can access one script from another, but before that, why not edit your question to show us what you're doing in the other script? How does the player rotation work? – Basic Oct 17 '22 at 18:32
  • Thank you so much for the help. I probably should edit my query and show how I'm rotating the player, but making xRotand yRot public and static did the trick! Omfg works so good. – Jason Burley Oct 18 '22 at 04:38
  • Comment again when you have and I'll show you how to integrate it into one script (or communicate between scripts if that's more appropriate) – Basic Oct 18 '22 at 23:09
0

It looks to me like you want to update your FPS Controller script with a public method something like this:

public void RotateCameraToFace(Vector3 direction) {
    Vector3 d = direction.normalized;
    xRot = Mathf.Asin(-d.y);
    yRot = Mathf.Atan2(d.x, d.z);
}

This updates the internal angle-tracking variables so they'll produce an orientation that looks along direction.

If you have your desired orientation as a quaternion, you can call controller.RotateCameraToFace(orientation * Vector3.forward) to use this same code.

You could also try unpacking xRot and yRot from orientation.eulerAngles, though I wouldn't recommend it. You can get some awkward wrap-around cases when using computed Euler angles as input for another computation, so my standard practice is to compute the angles myself in a way I can control and verify has wrap-arounds handled in a way that works for my use case.

DMGregory
  • 134,153
  • 22
  • 242
  • 357