- Home /
in-Game camera movement like editor
Hi, I was wondering. I want an in-game camera view/movement like in the editor and my question is if someone has an example or can give me some help. Because I have no idea how to make it.
Thanks in advance! :)
This is what i btw have, but the camera is rotating around its own axis.. And I want it like in the editor, that It rotates around the mouse Axis and zooms in at the mouse position
using UnityEngine;
using System.Collections;
public class cameraRot : MonoBehaviour {
Vector3 startPos;
Quaternion startRot;
void Awake () {
startPos = new Vector3 (this.transform.position.x, this.transform.position.y, this.transform.position.z);
startRot = new Quaternion (this.transform.rotation.x, this.transform.rotation.y, this.transform.rotation.z, this.transform.rotation.w);
}
void Update () {
CameraZoom ();
rotateObject ();
resetPosition ();
}
void resetPosition(){
if(Input.GetKey(KeyCode.R)){
this.transform.position = startPos;
this.transform.rotation = startRot;
}
}
void CameraZoom(){
float mouseScrollValue = Input.GetAxis ("Mouse ScrollWheel") * 100f;
float mouseScroll = Mathf.Clamp((int)mouseScrollValue, -10 ,20);
float mouseX = Input.mousePosition.x / 5;
float mouseY = transform.position.y;
float mouseZ = Input.mousePosition.y / 5;
Vector3 thePos = new Vector3(mouseX,mouseY,mouseZ);
if(Input.GetKey(KeyCode.Z)){ //Set Camera/Mouse Position to active
transform.position = thePos;
Camera.main.transform.position -= new Vector3 (0f, mouseScroll, 0f);
}
}
void rotateObject(){
float moveAxisX = Input.GetAxis ("Mouse X");
float moveAxisY = Input.GetAxis ("Mouse Y");
if (Input.GetKey (KeyCode.X)) {
//nogood
Camera.main.transform.Rotate(-moveAxisY,moveAxisX,0f);
}
}
}
Answer by Calum-McManus · Mar 18, 2014 at 12:36 PM
http://wiki.unity3d.com/index.php?title=MouseOrbitZoom
This is pretty close, give it ago! :)
Nice, this is pretty close to what I'm looking for! :)
Glad I could help! It has come in handy for me before :)
Is there any instructions to how it must be installed out there?, or i just drag in to the $$anonymous$$ainCamera?, because aparently, that doesn't work.
But pretty amazing found btw.
Answer by splitter20 · Oct 05, 2017 at 09:54 PM
works for me
public class CameraMovement : MonoBehaviour {
public float lookSpeedH = 2f;
public float lookSpeedV = 2f;
public float zoomSpeed = 2f;
public float dragSpeed = 6f;
private float yaw = 0f;
private float pitch = 0f;
void Update ()
{
//Look around with Right Mouse
if (Input.GetMouseButton(1))
{
yaw += lookSpeedH * Input.GetAxis("Mouse X");
pitch -= lookSpeedV * Input.GetAxis("Mouse Y");
transform.eulerAngles = new Vector3(pitch, yaw, 0f);
}
//drag camera around with Middle Mouse
if (Input.GetMouseButton(2))
{
transform.Translate(-Input.GetAxisRaw("Mouse X") * Time.deltaTime * dragSpeed, -Input.GetAxisRaw("Mouse Y") * Time.deltaTime * dragSpeed, 0);
}
//Zoom in and out with Mouse Wheel
transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * zoomSpeed, Space.Self);
}
}
THAN$$anonymous$$ YOU! I have been struggling for 2 days.
Answer by marceloroca · Oct 10, 2019 at 10:12 PM
some changes to work like Unity scene editor:
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
[SerializeField]
private float lookSpeedH = 2f;
[SerializeField]
private float lookSpeedV = 2f;
[SerializeField]
private float zoomSpeed = 2f;
[SerializeField]
private float dragSpeed = 3f;
private float yaw = 0f;
private float pitch = 0f;
private void Start()
{
// Initialize the correct initial rotation
this.yaw = this.transform.eulerAngles.y;
this.pitch = this.transform.eulerAngles.x;
}
private void Update()
{
// Only work with the Left Alt pressed
if (Input.GetKey(KeyCode.LeftAlt))
{
//Look around with Left Mouse
if (Input.GetMouseButton(0))
{
this.yaw += this.lookSpeedH * Input.GetAxis("Mouse X");
this.pitch -= this.lookSpeedV * Input.GetAxis("Mouse Y");
this.transform.eulerAngles = new Vector3(this.pitch, this.yaw, 0f);
}
//drag camera around with Middle Mouse
if (Input.GetMouseButton(2))
{
transform.Translate(-Input.GetAxisRaw("Mouse X") * Time.deltaTime * dragSpeed, -Input.GetAxisRaw("Mouse Y") * Time.deltaTime * dragSpeed, 0);
}
if (Input.GetMouseButton(1))
{
//Zoom in and out with Right Mouse
this.transform.Translate(0, 0, Input.GetAxisRaw("Mouse X") * this.zoomSpeed * .07f, Space.Self);
}
//Zoom in and out with Mouse Wheel
this.transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * this.zoomSpeed, Space.Self);
}
}
}