The question is answered, right answer was accepted
Make continuous camera rotation whilst holding the RMB
Hello everyone!
I am a complete beginner in Unity C# programming, I'm learning it to complement my skill in my game design course. I am currently working on a script to control the camera, so far I've achieved keyboard input with the standard WASD keys and zooming with the scroll wheel, but now I'm bumping into an issue I can't seem to resolve.
I'd like to rotate the camera following mouse movement (much like the standard asset First Person Controller) but only when the Right Mouse Button is held down. This is what I have so far:
Note: I have removed camera control code that is not relevant to the rotation for legibility purposes
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour
{
public float rotateSpeed;
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * rotateSpeed;
float mouseY = Input.GetAxis("Mouse Y") * rotateSpeed;
//Camera can be rotated only if the right mouse button is held down
if (Input.GetMouseButtonDown(1))
{
Debug.Log("Detected Right Mouse button");
transform.RotateAround(transform.position, transform.right, -mouseY);
transform.RotateAround(transform.position, Vector3.up, mouseX);
}
}
}
Now, the problem I am having is that the rotation is not continuous. The only way this works is to move the mouse, click RMB once and it will snap to the new rotation. I'd like it to follow mouse movement. Can anyone help me with the code and maybe explain what they did? Thanks!
A fix has been found:
I used Input.Get$$anonymous$$ouseButtonDown(1) which only detects on the frame itself, it's not a state detection. Use Input.Get$$anonymous$$ouseButton(1) ins$$anonymous$$d!