- Home /
How can I replace this Vector3 with a float?
Hi!
I am following a tutorial to get my player controller working. I finished the tutorial and started to add on things. When I got the camera rotation I needed to add some extra variables but that messes up the camera rotation. At around 14:26 in the video he makes the camera rotate by using 'cameraT.localEulerAngles = Vector3.left * verticalLookRotation;', and that works good. But not good enough for me. So firstly I tried to replace it with:
cameraHead.localEulerAngles = new Vector3(Vector3.left * verticalLook, cameraHead.rotation.y, inputSensitivity * Time.deltaTime);
But of course, that won't work since you can't seem to have a Vector3 in a new Vector3. So I later tried to remove the Vector3.left and keep the vertical look. That worked fine except the vertical look was inverted. So I later tried this:
cameraHead.localEulerAngles = new Vector3(Input.GetAxis("Mouse X") * verticalLook, cameraHead.rotation.y, inputSensitivity * Time.deltaTime);
Now that breaks the whole camera. It keeps rotating but at the same time trying to get back to the value of 0.
So basically, what can I do to get it working as I want it to work? Be able to look up and down and not it being inverted.
Thanks in advance!
If any further questions about my code, please ask. I did my best to explain all you need.
Answer by AurimasBlazulionis · Jun 20, 2015 at 09:24 PM
Where is verticalLook being declared? If it is some float, just use it without any Input.GetAxis or Vector3.left, because you are already setting value of x axis
Oh sorry. I missed to include that.
This is the code that declares the vertical look (float).
verticalLook += Input.GetAxis("$$anonymous$$ouse Y") * Time.deltaTime * mouseSettings.mouseSensitivityY;
verticalLook = $$anonymous$$athf.Clamp(verticalLook, mouseSettings.clamp$$anonymous$$inimumX, mouseSettings.clamp$$anonymous$$aximumX);
It is being declared right before the code from above.
I also tried to remove the "Input.GetAxis" and that's what is making the input inverted.
if it get's inverted, try using verticalLook -= blablabla, ins$$anonymous$$d of +=
@TheDiamondPlay Thanks. I can't believe that I didn't think about that...
Answer by KevinCodes4Food · Jun 20, 2015 at 09:33 PM
Hi RealMTG,
If I understand what you are trying to accomplish, you would like to have the mouse X axis input map to vertical movement on the camera.
The first key issue is that Input.GetAxis("Mouse X") only returns delta move values, and zero when the mouse is not moving. Assigning this directly to X rotation is why your camera rotation keeps going back to zero.
You'll need to store the mouse X movement in an accumulator that retains its value across Update() calls to add to or remove from the mouse look each frame. Probably something like this:
float mouseAccumulator = 0;
void Update () {
...
mouseAccumulator += Input.GetAxis("Mouse X") * inputSensitivity;
mouseAccumulator = Mathf.Clamp(mouseAccumulator, -89, 89);
float x = mouseAccumulator;
float y = cameraHead.transform.eulerAngles.y;
float z = inputSensitivity * Time.deltaTime;
cameraHead.transform.localEulerAngles = new Vector3(x,y,z);
...
}
Second issue is that typically the local Euler angle vector is treated as an X, Y, Z rotation. Rotation on X is looking up/down, Y left/right, and Z rotating like a barrel roll. Your code is using mouse X, left right, to go up/down, which is odd. Also, your formula is setting Z to a not quite zero value each frame, which is odd, too. So I am guessing that what you really would want to do something like this:
Vector3 lookDirectionVector = Vector3.zero;
void Update () {
...
lookDirectionVector.x += Input.GetAxis("Mouse Y") * inputSensitivity; // Up & Down
lookDirectionVector.y += Input.GetAxis("Mouse X") * inputSensitivity; // Left & Right
lookDirectionVector.z = 0; // Roll
lookDirectionVector.x = Mathf.Clamp(lookDirectionVector.x, -89, 89);
cameraHead.transform.localEulerAngles = lookDirectionVector;
...
}
Finally, if you need to invert up/down movement, just change the X vector to -= instead of +=.
Your answer
Follow this Question
Related Questions
Accuracy issues with Float (Vector3) 2 Answers
Play incorrect animations when is looking in other direction 0 Answers
Connecting transform with Vector3 4 Answers
How can i rotate this vector over time? 2 Answers
How do you perform multiple rotations on the same object? - (in a single frame) 0 Answers