Click and drag to rotate camera, like a pan?
Hello,
For an application I am trying to make a first person camera that rotates around it's own axis to look around, so the camera stands still. To rotate the camera/view around the user would move the mouse somewhere on the screen, click and then drag to move the camera. Exactly the same way that Google Street View handles it.
I am new to coding more advanced languages involing vectors, quaternions and what not, and I understand that some of what needs to happen is to calculate distances and differences between the mouse starting position and moving position in relation to the camera, but I am having a difficulty wrapping my head around it all?
I have looked and looked for others trying to do the same very few came up, and none yielded any results.
Any help and or suggestions on where to find some information, would be much apprecieated.
Answer by Marlon_Braga · Jul 06, 2017 at 10:01 PM
This work for me
Just attatch on Camera
using UnityEngine;
public class StreetViewCamera:MonoBehaviour {
public float speed = 3.5f;
private float X;
private float Y;
void Update() {
if(Input.GetMouseButton(0)) {
transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * speed, -Input.GetAxis("Mouse X") * speed, 0));
X = transform.rotation.eulerAngles.x;
Y = transform.rotation.eulerAngles.y;
transform.rotation = Quaternion.Euler(X, Y, 0);
}
}
}
This works great in the Unity Editor. What would the equivalent look like if using a touch screen?
Answer by FortisVenaliter · May 20, 2016 at 06:24 PM
You should check out the MouseLook script in the Standard Asset FirstPersonController. What you're describing is virtually identical... you'd just need to check if the mouse button is down to activate it.
Thanks so much for a response! I found the $$anonymous$$ouseLook script you were talking about, but I can't attatch the script to anything, it gives me an error of "Can't add script, needs to be derive of $$anonymous$$onoBehaviour" I don't know which parts of the script to alter. I assume some of the declarations in the beginning of the script needs to be change, so it becomes a derive of $$anonymous$$onoBehaviour?
If you intend to use it as one, yes. Or you can use it as a subcomponent of another script like FirstPersonController. It's entirely up to you how you code your game.
Okay, I have added : "$$anonymous$$onoBehaviour" to the public class $$anonymous$$ouseLook. Then I have attatched the script to my $$anonymous$$ain Camera, but no mouse movement is registered when moving the mouse, I haven't tried to implement any Get$$anonymous$$ouseButtonDown, first I wan't to see if I can get the $$anonymous$$ouseLook script to work on it's own, but I can't. I assume just adding the $$anonymous$$onoBehaviour doesn't work? Perhaps it's dependent on a character controller or something like that?
Answer by hydrix · Sep 22, 2016 at 09:09 PM
Camera.main.transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X")*rotateSpeed, 0), Space.World);
Your answer
Follow this Question
Related Questions
getting maximum x and y values of mouse position 1 Answer
How to detect how much was mouse moved only horizontaly? 1 Answer
How to Instantiate from mouse pos? 0 Answers
Camera controls not working quite right 0 Answers
Object Rotate with Camera 2 Answers