Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to click and drag to Mouse Orbit in Unity

Posted on December 4, 2019August 4, 2020 by Habibie

In many cases we need an ability to do Mouse Orbit in our Unity Game. Mouse Orbit means by moving your mouse you are orbiting a specific object to view it in different angles.

I’ve found this script from this link: https://wiki.unity3d.com/index.php/MouseOrbitImproved

By default, without clicking the screen you already orbiting your specific object. But I modified the script to only orbit by mouse click only, I mean if I don’t hold my left mouse button I don’t want to orbit.

Here is the modified script:

/*
Original script from: https://wiki.unity3d.com/index.php/MouseOrbitImproved
Just a few line modification by me: https://www.youtube.com/channel/UCjAFR8Gm-7GsMvtC_scsp8A/
*/ 


using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour
{

    public Transform target;
    public float distance = 5.0f;
    public float xSpeed = 120.0f;
    public float ySpeed = 120.0f;

    public float yMinLimit = -20f;
    public float yMaxLimit = 80f;

    public float distanceMin = .5f;
    public float distanceMax = 15f;

    private Rigidbody rigidbody;

    float x = 0.0f;
    float y = 0.0f;

    bool orbitable;

    // Use this for initialization
    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;

        rigidbody = GetComponent<Rigidbody>();

        // Make the rigid body not change rotation
        if (rigidbody != null)
        {
            rigidbody.freezeRotation = true;
        }
    }

    void LateUpdate()
    {
        /*
        if (Input.GetKeyDown(KeyCode.LeftControl) == true)
        {
            orbitable = true;
        }
        else if (Input.GetKeyUp(KeyCode.LeftControl) == true)
        {
            orbitable = false;
        }
        */

        if (Input.GetMouseButtonDown(0))
            orbitable = true;
        else if (Input.GetMouseButtonUp(0))
            orbitable = false;

        if (orbitable) { 
            if (target)
            {
                x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                y = ClampAngle(y, yMinLimit, yMaxLimit);

                Quaternion rotation = Quaternion.Euler(y, x, 0);

                distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);

                RaycastHit hit;
                if (Physics.Linecast(target.position, transform.position, out hit))
                {
                    distance -= hit.distance;
                }
                Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
                Vector3 position = rotation * negDistance + target.position;

                transform.rotation = rotation;
                transform.position = position;
            }
        }
    }

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
}

If you wish to download a sample scene for this example, click this link: https://drive.google.com/open?id=1ICG4-FQ8MrPwFWNIzeyuh75qcw20iq7-

That link contains a UnityPackage to be imported to Unity.

Post Views: 638
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme