- Home /
[Solved :D] Camera auto turns 180° from its init position on Play (Animated GIF Included!)
Exactly as the title says: I set my camera to face a certain direction upon start and it turns (yaw) exactly 180° from that once I press play. In my simple mouselook script, I tried making a Start() function setting these exact initial Euler vector coords for testing, and it would start correctly, then snap the other direction as soon as a frame goes by.
(Animated gif of the issue at the bottom of this post)
This leads me to believe it has something to do with the line in my Rotation Application section of Update (there is no Start now, as that was just for testing; the initial values of this line are not set in the variable declarations; hence why I've only included the pertinent Update function):
transform.rotation = Quaternion.Euler(currentPitch, currentYaw, 0);
/* UPDATE (CALLED ONCE EVERY FRAME) */
void Update () {
// Pitch w/ Inversion Code
if (Input.GetKeyDown("i")){ //If 'i' key is pressed
inverted = !inverted; //invert if not inverted (default), and vice-versa
}
if (inverted)
{
targetPitchRot += Input.GetAxis("Mouse Y") * lookSens; //inverted
}
else
{
targetPitchRot -= Input.GetAxis("Mouse Y") * lookSens; //not inverted
}
// Yaw
targetYawRot += Input.GetAxis("Mouse X") * lookSens;
// Smoothing
currentPitch = Mathf.SmoothDamp(currentPitch, targetPitchRot, ref lookSmoothXVel, lookSmoothDamp);
currentYaw = Mathf.SmoothDamp(currentYaw, targetYawRot, ref lookSmoothYVel, lookSmoothDamp);
// Rotation Application
transform.rotation = Quaternion.Euler(currentPitch, currentYaw, 0);
} /* ENDUPDATE. */
You have to set the values of current$$anonymous$$ch
and currentYaw
in a Start
function.
Try something like :
void Start()
{
current$$anonymous$$ch = transform.eulerAngles.x ;
currentYaw= transform.eulerAngles.y ;
}
Yeah I was thinking along the same lines, since the compiler arrives at the rotation orientation at the end of Update (and by this point, if the player doesn't touch the mouse, the values will be (0,0,0), so that all makes sense) and isn't really told what to do before then. However, I've tried your code, I've tried manually setting currentYaw=130; (which is how I got it to pause for a second before the smoothing in my "bug" post) and still the same thing over and over. Like I said, changing it locally worked (even though it shouldn't have lol I was not thinking clearly) until I saved and booted the project back up.
$$anonymous$$aybe my compiler had a glimpse into empathy for human logic and I killed it when I shut down Unity xD
Edit: & I gotta say a huge THAN$$anonymous$$ YOU for even responding! TWICE!! You're awesome. Just to have someone else's input has eased my frustration of going insane for the past 8-9 hours now.
Oh.. actually 10 hours. just checked lmao
Answer by Hellium · Nov 28, 2017 at 05:27 PM
My bad, you have to assign the values to targetPitchRot and targetYawRot too :
void Start()
{
currentPitch = targetPitchRot = transform.eulerAngles.x ;
currentYaw = targetYawRot = transform.eulerAngles.y ;
}
I was just about to say at this point I'm probably going to feel really dumb when I finally figure it out, but I don't think I would've. Thank you so so so so so so so so so so so so so so so so so sososo muchhhhh dude!! IT WOR$$anonymous$$SSSSSS!!!! :D
Edit*:If you'd like to repost that last comment as an answer, I'll gladly accept it. Thank you so much again I have half the $$anonymous$$d to weep at this conclusion of one long & stressful night lol