- Home /
Problem With First Person View Controller
I've been working on a View Controller but for some reason when the mouse is moved horizontally or diagonally the view tilts.
Here is the code I wrote:
using UnityEngine;
using System.Collections;
public class ViewControl : MonoBehaviour {
void FixedUpdate ()
{
float ViewH = Input.GetAxis("Mouse X");
float ViewV = Input.GetAxis("Mouse Y");
ViewManagement (ViewH, ViewV);
}
void ViewManagement(float ViewH, float ViewV)
{
if (ViewH < 0f)
{
transform.Rotate(0,1,0);
}
else if (ViewH > 0f)
{
transform.Rotate(0,-1,0);
}
//
if (ViewV < 0f)
{
transform.Rotate(1,0,0);
}
else if (ViewV > 0f)
{
transform.Rotate(-1,0,0);
}
}
}
If anyone could help that would be much appreciated.
Answer by Al_Goyle · Jan 22, 2016 at 11:01 AM
The view tilts because you don't make use of the analog input of the mouse. The horizontal and vertical are not either 0 or 1, but the rotation is.... Also, the multiple independent calls to Rotate may add up to the result. It is generally better to read the player inputs and then apply the rotation once per frame (or physics frame).
I don't know if reading the inputs in FixedUpdate could be a problem too by the way, because it fires less often than Update and you could miss an input.
Anyway, here is some untested code:
private float rotateSpeed = 100f;
void Update () {
Vector3 mouseInput = new Vector3 (Input.GetAxis("Horizontal", Input.GetAxis("Vertical", 0);
transform.Rotate(mouseInput * Time.deltaTime * rotateSpeed);
}
Your answer
Follow this Question
Related Questions
Restricting camera movement in first person camera? 0 Answers
Embed Unity game in a ViewController within my ios app. 1 Answer
How do I make a crosshair? 2 Answers
How can I use only keyboard in FPC? 0 Answers
Click on things? 0 Answers