free camera movement

Hi all

how make a free camera movement, not look around.

i find only scripts where have arrow keys or WASD

i need:
Move Camera in X and Z Axis with Mouse and right Mouse Button

here a script where look around.

using UnityEngine;
using System.Collections;

public class Maus : MonoBehaviour {

     float deltaRotation = 50f;

     // Use this for initialization
     void Start () {

     }

     // Update is called once per frame
     void Update () {

          if(Input.GetAxis("Mouse X") < 0){
          //Code for action on mouse moving left
               transform.Rotate (new Vector3 (0f, -deltaRotation, 0f) * Time.deltaTime);
          }
          else if(Input.GetAxis("Mouse X") > 0){
          //Code for action on mouse moving right
               transform.Rotate (new Vector3 (0f, deltaRotation, 0f) * Time.deltaTime);
          }

          if(Input.GetAxis("Mouse Y") < 0){
          //Code for action on mouse moving left
               transform.Rotate (new Vector3 (deltaRotation, 0f, 0f) * Time.deltaTime);
          }
          else if(Input.GetAxis("Mouse Y") > 0){
          //Code for action on mouse moving right
               transform.Rotate (new Vector3 (-deltaRotation, 0f, 0f) * Time.deltaTime);
          }
     }
}

Transform.Translate or just add vectors to transform.position

And I found this forum post describing what I think you mean regarding the mouse movement.

1 Like

thx for the link, will test tomorrow :slight_smile:

This whole process might be easier for you with cinemachine. I think then it becomes a case of simply moving the viewpoint and what it looks at.

There’s even a dedicated forum: Unity Engine - Unity Discussions

Is this something you’re looking for?
https://github.com/ZhengzhongSun/TrackballCamera

i find in the post this script, work fine :slight_smile:

using UnityEngine;
public class CameraPanControl : MonoBehaviour
{
    // Original code sourced from LostConjugate & Matti Jokihaara via
    // https://www.youtube.com/watch?v=mJCbEL5J5fg
    float dragSpeed = 15f; // Found this by trial and error - applied below in Update
    float scale;
    float normalizedScale = 250f; // Found this by trial and error - applied below in Update
    void Update()
    {
        Vector3 pos = transform.position;
        scale = Camera.main.orthographicSize;  // Use this to capture the zoom level
        if (Input.GetMouseButton(2))
        {
            pos.x -= Input.GetAxis("Mouse X") * dragSpeed * scale / normalizedScale;
            pos.z -= Input.GetAxis("Mouse Y") * dragSpeed * scale / normalizedScale;
        }
        transform.position = pos;
    }
}