- Home /
Why is my camera script inverted
After writing the code for the camera script,Once I click and hold the right mouse button to look around,It just goes all inverted and i hate inverted.
Can someone please look at my script to see whats wrong?
That'd be great!
using System.Collections;
using UnityEngine;
public class CameraMovement : MonoBehaviour {
public float horispeed = 3f;
public float vertspeed = 3f;
void Update () {
if (Input.GetMouseButton (1)) {
float vertical = vertspeed * Input.GetAxis ("Mouse X");
float horizontal = horispeed * Input.GetAxis ("Mouse Y");
transform.Rotate (-vertical, horizontal, 0);
float z = transform.eulerAngles.z;
transform.Rotate(0,0,-z);
}
}
}
Answer by Tobychappell · May 25, 2018 at 06:54 PM
Create a public bool 'invertInput' and two properties that return the horizontal and vertical speeds and use them in the block of code inside the Update method. If invertInput is true then flip the sign of the speed value before you return it.
public class CameraMovement : MonoBehaviour
{
public float horispeed = 3f;
public float vertspeed = 3f;
public bool invertInput;
public float HorizontalSpeed
{
get
{
return Mathf.Abs(horispeed) * (invertInput ? -1 : 1);
}
}
public float VerticalSpeed
{
get
{
return Mathf.Abs(vertspeed) * (invertInput ? -1 : 1);
}
}
void Update()
{
if (Input.GetMouseButton(1))
{
float vertical = HorizontalSpeed * Input.GetAxis("Mouse X");
float horizontal = VerticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(-vertical, horizontal, 0);
float z = transform.eulerAngles.z;
transform.Rotate(0, 0, -z);
}
}
}
$$anonymous$$ight it be possible to show what you mean by this.I'm very confused about what you said
Sure, I've updated my answer with example code. If you don't want the horizontal component to be inverted then you can remove the "*(invertInput ? -1 : 1)" inside HorizontalSpeed.
Also, i don't know if you've done this on purpose but you have sideways movement of the mouse affecting the pitch and the back and forth movement of the mouse affecting the yaw.
Basically i think you have your Input.GetAxis("...") the wrong way round
THAN$$anonymous$$ YOU! I DONT $$anonymous$$NOW WHY BUT I ADDED CO$$anonymous$$$$anonymous$$ENT IN $$anonymous$$Y SCRIPT THAN$$anonymous$$ING YOU!
I ALSO GAVE YOU A POINT!
YOU ARE THE BEST!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cinemachine freelook cam Crossplatform Input Movement 0 Answers
Dissable lights per camera? 2 Answers