- Home /
Keep the scene view rotation of my camera in play mode (it resets to 0) [EXAM ^^]
I have a script for the camera movement (FPS) that works perfectly. The thing is, in my scene view, I place my player and the camera so I have a beautiful establishing shot for my game but when I hit play, the rotation of the player and the camera resets to zero, so I start the game with a... not so nice first image. Any idea ?
Here's the (only) script that takes charge of the rotation of my camera and my capsule.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerCamera : MonoBehaviour {
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
Vector2 camLook;
Vector2 smoothVector;
GameObject characterCapsule;
void Start () {
characterCapsule = this.transform.parent.gameObject;
}
void Update () {
var md = new Vector2 (Input.GetAxisRaw ("RightStick_X"), Input.GetAxisRaw ("RightStick_Y"));
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
// Lerp allows the value to change smoothly (no abrupt stop nor start);
smoothVector.x = Mathf.Lerp (smoothVector.x, md.x, 1f / smoothing);
smoothVector.y = Mathf.Lerp (smoothVector.y, md.y, 1f / smoothing);
camLook.y = Mathf.Clamp(camLook.y, -65f, 65f);
camLook += smoothVector;
transform.localRotation = Quaternion.AngleAxis (-camLook.y, Vector3.right);
characterCapsule.transform.localRotation = Quaternion.AngleAxis (camLook.x, characterCapsule.transform.up);
}
}
Answer by Bunny83 · Jun 02, 2018 at 10:31 PM
I guess you mean the gameview, not the sceneview? The sceneview is just the editor view. When you start your game Unity switches to the gameview.
Anyways you do not initialize your "camLook" vector so it defaults to (0, 0). If you want to apply the current rotation at start try something like this inside Start:
camLook.y = transform.localEulerAngles.x;
camLook.x = characterCapsule.transform.localEulerAngles.y;
Apart from this your "scaling" in line 23 could be simplified to
md *= sensitivity * smoothing;
Even when you want seperate factors for x and y this would be way better:
md.x *= sensitivity * smoothing;
md.y *= sensitivity * smoothing;
Finally you should swap those two lines (#27 and #28):
camLook.y = Mathf.Clamp(camLook.y, -65f, 65f);
camLook += smoothVector;
Add the change first and do the clamping after the change. Otherwise you get some jittering at the clamplimit.
ps: I don't think the last line (#31) is correct. You apply a local rotation but the rotation is around a worldspace axis. Maybe in your case it works but this generally seem to be wrong. It seems you just want to rotate around Vector3.up but it's hard to tell without knowing the exact setup of the gameobjects
@Bunny83 Thank you for the quick reply :) I've changed the lines you pointed out for optimisation, thanks :). (I did mean the scene view, with the editor and everything. I choose a position for my player there, and when I hit play, the position is reset).
Unfortunately, the second line to apply the current rotation returns an error ("... Unity engine.tranform does not contain a definition for y..."
I made this script following a tutorial, and while I understand the general idea of what it does, I do not have the capacity to explain really well the last line. All I can say is that it works. But thanks for pointing that out, it's always good for my knowledge to know that :)
Ok so, I tested it and the rotation of the capsule works just perfectly. :) Thanks! But the camera snaps to -65 for its x rotation while it should be - 22. I suppose it has something to do with the clamp since it's -65 too, and indeed, when I change the clamp value, the camera x rotation snaps to the new clamp value. I think understand l what the line camLook.y = transform.localEulerAngles.x; does but I dont see why it would snap the the clamp value since -22 is within the range. I've tried many x value (within or outside the clamp range) and it turns out it's completely random (at least it is for me). The only thing I know is when I put a negative value whithin the range, it snaps to -65.
Your answer
Follow this Question
Related Questions
Rotation resetted on start 1 Answer
How to rotate FPSController in mobile? 1 Answer
When I hit play the character rotates 2 Answers
Minor scale issue on play mode 0 Answers
horizontal recoil reset with slerp 1 Answer