C# with Unity 3D: How do I make a camera move around an object when user moves mouse










1














I am trying to make a 3d viewing simulation in Unity 4 where the user can select an object and move their mouse to rotate around it (360 degrees) I have taken many shots to try get it to work, but I fail each time, any help will be appreciated and if it is written in C# that would be great! (But it doesn't have to)
Thanks in advance!










share|improve this question























  • I can't understand what you are saying,//user can select an object and move their mouse to rotate around it // part.can you explain it further?
    – AVI
    Dec 6 '15 at 13:16















1














I am trying to make a 3d viewing simulation in Unity 4 where the user can select an object and move their mouse to rotate around it (360 degrees) I have taken many shots to try get it to work, but I fail each time, any help will be appreciated and if it is written in C# that would be great! (But it doesn't have to)
Thanks in advance!










share|improve this question























  • I can't understand what you are saying,//user can select an object and move their mouse to rotate around it // part.can you explain it further?
    – AVI
    Dec 6 '15 at 13:16













1












1








1







I am trying to make a 3d viewing simulation in Unity 4 where the user can select an object and move their mouse to rotate around it (360 degrees) I have taken many shots to try get it to work, but I fail each time, any help will be appreciated and if it is written in C# that would be great! (But it doesn't have to)
Thanks in advance!










share|improve this question















I am trying to make a 3d viewing simulation in Unity 4 where the user can select an object and move their mouse to rotate around it (360 degrees) I have taken many shots to try get it to work, but I fail each time, any help will be appreciated and if it is written in C# that would be great! (But it doesn't have to)
Thanks in advance!







c# object unity3d camera mouse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '15 at 17:11









mwilczynski

2,20411223




2,20411223










asked Dec 6 '15 at 12:48









MSpace-Dev

2817




2817











  • I can't understand what you are saying,//user can select an object and move their mouse to rotate around it // part.can you explain it further?
    – AVI
    Dec 6 '15 at 13:16
















  • I can't understand what you are saying,//user can select an object and move their mouse to rotate around it // part.can you explain it further?
    – AVI
    Dec 6 '15 at 13:16















I can't understand what you are saying,//user can select an object and move their mouse to rotate around it // part.can you explain it further?
– AVI
Dec 6 '15 at 13:16




I can't understand what you are saying,//user can select an object and move their mouse to rotate around it // part.can you explain it further?
– AVI
Dec 6 '15 at 13:16












4 Answers
4






active

oldest

votes


















1














The MouseOrbit script do that:



http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23



Just attach this script into your Camera Object, and link the target object in inspector.






share|improve this answer




























    2














    This is a different and interesting way :) (I use it)



    Screenshot



    (Here, the cube is the target)



    1) Create sphere - Name: "Camera Orbit" - Add material: Transparent (Alpha = 0) - As scale as you want - Rotation: (0,0,0.1f)

    2) Add the camera as a "child" to Camera Orbit's surface. Position = (0,"y = camera orbit scale",0)
    Rotation = (90,0,0)

    3) Create empty GameObject - Name: Input Control.



    InputControl.cs:



    public class InputControl : MonoBehaviour

    public GameObject cameraOrbit;

    public float rotateSpeed = 8f;

    void Update()

    if (Input.GetMouseButton(0))
    cameraOrbit.transform.eulerAngles.z + v >= 179.9f)
    v = 0;

    cameraOrbit.transform.eulerAngles = new Vector3(cameraOrbit.transform.eulerAngles.x, cameraOrbit.transform.eulerAngles.y + h, cameraOrbit.transform.eulerAngles.z + v);


    float scrollFactor = Input.GetAxis("Mouse ScrollWheel");

    if (scrollFactor != 0)

    cameraOrbit.transform.localScale = cameraOrbit.transform.localScale * (1f - scrollFactor);






    CameraController.cs:



    public class CameraController : MonoBehaviour

    public Transform cameraOrbit;
    public Transform target;

    void Start()

    cameraOrbit.position = target.position;


    void Update()

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

    transform.LookAt(target.position);




    4) Add CameraController.cs to Camera.

    5) Add InputControl.cs to Input Control.

    6) Set public variables in scripts. ("Camera Orbit" and "Target")



    That's all. Mouse click and drag: Rotate - Mouse whell: Zoom in-out.



    ps. If you want, you can change target as runtime.






    share|improve this answer






























      0














      This is perfect. The only change I made is to add a script to the Camera Orbit:



      public class FollowPlayer : MonoBehaviour 

      public GameObject player;
      private Vector3 playerPos;

      // Update is called once per frame
      void Update ()
      if (this.transform.localScale.x <= 1)

      this.transform.localScale = new Vector3(1, 1, 1);


      if (this.transform.localScale.x >= 15)

      this.transform.localScale = new Vector3(15, 15, 15);


      playerPos = player.transform.position;
      this.transform.position = playerPos;




      Then attach your "player" object to the Input Control, and the input control will go where ever the player does, allowing you to track the player, as well as rotate and mouse wheel zoom. Fancy.



      The localScale if statements means you can only zoom in and out so far.



      The only problem with this script now is that if you zoom out to 15 and then keep trying to zoom out, the camera bounces. I'm sure that's an easy fix, though, I just haven't put the time in yet.






      share|improve this answer




























        0














        You don't need the CameraController at all, just set the camera's z rotation to -90.






        share|improve this answer
















        • 1




          (This post does not seem to provide a quality answer to the question. Please either edit your answer and improve it, or just post it as a comment to the question.)
          – sɐunıɔןɐqɐp
          Oct 3 at 9:26










        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34117591%2fc-sharp-with-unity-3d-how-do-i-make-a-camera-move-around-an-object-when-user-mo%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        The MouseOrbit script do that:



        http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23



        Just attach this script into your Camera Object, and link the target object in inspector.






        share|improve this answer

























          1














          The MouseOrbit script do that:



          http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23



          Just attach this script into your Camera Object, and link the target object in inspector.






          share|improve this answer























            1












            1








            1






            The MouseOrbit script do that:



            http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23



            Just attach this script into your Camera Object, and link the target object in inspector.






            share|improve this answer












            The MouseOrbit script do that:



            http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23



            Just attach this script into your Camera Object, and link the target object in inspector.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 7 '15 at 18:37









            Ricardo Reiter

            522210




            522210























                2














                This is a different and interesting way :) (I use it)



                Screenshot



                (Here, the cube is the target)



                1) Create sphere - Name: "Camera Orbit" - Add material: Transparent (Alpha = 0) - As scale as you want - Rotation: (0,0,0.1f)

                2) Add the camera as a "child" to Camera Orbit's surface. Position = (0,"y = camera orbit scale",0)
                Rotation = (90,0,0)

                3) Create empty GameObject - Name: Input Control.



                InputControl.cs:



                public class InputControl : MonoBehaviour

                public GameObject cameraOrbit;

                public float rotateSpeed = 8f;

                void Update()

                if (Input.GetMouseButton(0))
                cameraOrbit.transform.eulerAngles.z + v >= 179.9f)
                v = 0;

                cameraOrbit.transform.eulerAngles = new Vector3(cameraOrbit.transform.eulerAngles.x, cameraOrbit.transform.eulerAngles.y + h, cameraOrbit.transform.eulerAngles.z + v);


                float scrollFactor = Input.GetAxis("Mouse ScrollWheel");

                if (scrollFactor != 0)

                cameraOrbit.transform.localScale = cameraOrbit.transform.localScale * (1f - scrollFactor);






                CameraController.cs:



                public class CameraController : MonoBehaviour

                public Transform cameraOrbit;
                public Transform target;

                void Start()

                cameraOrbit.position = target.position;


                void Update()

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

                transform.LookAt(target.position);




                4) Add CameraController.cs to Camera.

                5) Add InputControl.cs to Input Control.

                6) Set public variables in scripts. ("Camera Orbit" and "Target")



                That's all. Mouse click and drag: Rotate - Mouse whell: Zoom in-out.



                ps. If you want, you can change target as runtime.






                share|improve this answer



























                  2














                  This is a different and interesting way :) (I use it)



                  Screenshot



                  (Here, the cube is the target)



                  1) Create sphere - Name: "Camera Orbit" - Add material: Transparent (Alpha = 0) - As scale as you want - Rotation: (0,0,0.1f)

                  2) Add the camera as a "child" to Camera Orbit's surface. Position = (0,"y = camera orbit scale",0)
                  Rotation = (90,0,0)

                  3) Create empty GameObject - Name: Input Control.



                  InputControl.cs:



                  public class InputControl : MonoBehaviour

                  public GameObject cameraOrbit;

                  public float rotateSpeed = 8f;

                  void Update()

                  if (Input.GetMouseButton(0))
                  cameraOrbit.transform.eulerAngles.z + v >= 179.9f)
                  v = 0;

                  cameraOrbit.transform.eulerAngles = new Vector3(cameraOrbit.transform.eulerAngles.x, cameraOrbit.transform.eulerAngles.y + h, cameraOrbit.transform.eulerAngles.z + v);


                  float scrollFactor = Input.GetAxis("Mouse ScrollWheel");

                  if (scrollFactor != 0)

                  cameraOrbit.transform.localScale = cameraOrbit.transform.localScale * (1f - scrollFactor);






                  CameraController.cs:



                  public class CameraController : MonoBehaviour

                  public Transform cameraOrbit;
                  public Transform target;

                  void Start()

                  cameraOrbit.position = target.position;


                  void Update()

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

                  transform.LookAt(target.position);




                  4) Add CameraController.cs to Camera.

                  5) Add InputControl.cs to Input Control.

                  6) Set public variables in scripts. ("Camera Orbit" and "Target")



                  That's all. Mouse click and drag: Rotate - Mouse whell: Zoom in-out.



                  ps. If you want, you can change target as runtime.






                  share|improve this answer

























                    2












                    2








                    2






                    This is a different and interesting way :) (I use it)



                    Screenshot



                    (Here, the cube is the target)



                    1) Create sphere - Name: "Camera Orbit" - Add material: Transparent (Alpha = 0) - As scale as you want - Rotation: (0,0,0.1f)

                    2) Add the camera as a "child" to Camera Orbit's surface. Position = (0,"y = camera orbit scale",0)
                    Rotation = (90,0,0)

                    3) Create empty GameObject - Name: Input Control.



                    InputControl.cs:



                    public class InputControl : MonoBehaviour

                    public GameObject cameraOrbit;

                    public float rotateSpeed = 8f;

                    void Update()

                    if (Input.GetMouseButton(0))
                    cameraOrbit.transform.eulerAngles.z + v >= 179.9f)
                    v = 0;

                    cameraOrbit.transform.eulerAngles = new Vector3(cameraOrbit.transform.eulerAngles.x, cameraOrbit.transform.eulerAngles.y + h, cameraOrbit.transform.eulerAngles.z + v);


                    float scrollFactor = Input.GetAxis("Mouse ScrollWheel");

                    if (scrollFactor != 0)

                    cameraOrbit.transform.localScale = cameraOrbit.transform.localScale * (1f - scrollFactor);






                    CameraController.cs:



                    public class CameraController : MonoBehaviour

                    public Transform cameraOrbit;
                    public Transform target;

                    void Start()

                    cameraOrbit.position = target.position;


                    void Update()

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

                    transform.LookAt(target.position);




                    4) Add CameraController.cs to Camera.

                    5) Add InputControl.cs to Input Control.

                    6) Set public variables in scripts. ("Camera Orbit" and "Target")



                    That's all. Mouse click and drag: Rotate - Mouse whell: Zoom in-out.



                    ps. If you want, you can change target as runtime.






                    share|improve this answer














                    This is a different and interesting way :) (I use it)



                    Screenshot



                    (Here, the cube is the target)



                    1) Create sphere - Name: "Camera Orbit" - Add material: Transparent (Alpha = 0) - As scale as you want - Rotation: (0,0,0.1f)

                    2) Add the camera as a "child" to Camera Orbit's surface. Position = (0,"y = camera orbit scale",0)
                    Rotation = (90,0,0)

                    3) Create empty GameObject - Name: Input Control.



                    InputControl.cs:



                    public class InputControl : MonoBehaviour

                    public GameObject cameraOrbit;

                    public float rotateSpeed = 8f;

                    void Update()

                    if (Input.GetMouseButton(0))
                    cameraOrbit.transform.eulerAngles.z + v >= 179.9f)
                    v = 0;

                    cameraOrbit.transform.eulerAngles = new Vector3(cameraOrbit.transform.eulerAngles.x, cameraOrbit.transform.eulerAngles.y + h, cameraOrbit.transform.eulerAngles.z + v);


                    float scrollFactor = Input.GetAxis("Mouse ScrollWheel");

                    if (scrollFactor != 0)

                    cameraOrbit.transform.localScale = cameraOrbit.transform.localScale * (1f - scrollFactor);






                    CameraController.cs:



                    public class CameraController : MonoBehaviour

                    public Transform cameraOrbit;
                    public Transform target;

                    void Start()

                    cameraOrbit.position = target.position;


                    void Update()

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

                    transform.LookAt(target.position);




                    4) Add CameraController.cs to Camera.

                    5) Add InputControl.cs to Input Control.

                    6) Set public variables in scripts. ("Camera Orbit" and "Target")



                    That's all. Mouse click and drag: Rotate - Mouse whell: Zoom in-out.



                    ps. If you want, you can change target as runtime.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 27 at 14:44

























                    answered Feb 26 at 21:06









                    sadrag

                    618




                    618





















                        0














                        This is perfect. The only change I made is to add a script to the Camera Orbit:



                        public class FollowPlayer : MonoBehaviour 

                        public GameObject player;
                        private Vector3 playerPos;

                        // Update is called once per frame
                        void Update ()
                        if (this.transform.localScale.x <= 1)

                        this.transform.localScale = new Vector3(1, 1, 1);


                        if (this.transform.localScale.x >= 15)

                        this.transform.localScale = new Vector3(15, 15, 15);


                        playerPos = player.transform.position;
                        this.transform.position = playerPos;




                        Then attach your "player" object to the Input Control, and the input control will go where ever the player does, allowing you to track the player, as well as rotate and mouse wheel zoom. Fancy.



                        The localScale if statements means you can only zoom in and out so far.



                        The only problem with this script now is that if you zoom out to 15 and then keep trying to zoom out, the camera bounces. I'm sure that's an easy fix, though, I just haven't put the time in yet.






                        share|improve this answer

























                          0














                          This is perfect. The only change I made is to add a script to the Camera Orbit:



                          public class FollowPlayer : MonoBehaviour 

                          public GameObject player;
                          private Vector3 playerPos;

                          // Update is called once per frame
                          void Update ()
                          if (this.transform.localScale.x <= 1)

                          this.transform.localScale = new Vector3(1, 1, 1);


                          if (this.transform.localScale.x >= 15)

                          this.transform.localScale = new Vector3(15, 15, 15);


                          playerPos = player.transform.position;
                          this.transform.position = playerPos;




                          Then attach your "player" object to the Input Control, and the input control will go where ever the player does, allowing you to track the player, as well as rotate and mouse wheel zoom. Fancy.



                          The localScale if statements means you can only zoom in and out so far.



                          The only problem with this script now is that if you zoom out to 15 and then keep trying to zoom out, the camera bounces. I'm sure that's an easy fix, though, I just haven't put the time in yet.






                          share|improve this answer























                            0












                            0








                            0






                            This is perfect. The only change I made is to add a script to the Camera Orbit:



                            public class FollowPlayer : MonoBehaviour 

                            public GameObject player;
                            private Vector3 playerPos;

                            // Update is called once per frame
                            void Update ()
                            if (this.transform.localScale.x <= 1)

                            this.transform.localScale = new Vector3(1, 1, 1);


                            if (this.transform.localScale.x >= 15)

                            this.transform.localScale = new Vector3(15, 15, 15);


                            playerPos = player.transform.position;
                            this.transform.position = playerPos;




                            Then attach your "player" object to the Input Control, and the input control will go where ever the player does, allowing you to track the player, as well as rotate and mouse wheel zoom. Fancy.



                            The localScale if statements means you can only zoom in and out so far.



                            The only problem with this script now is that if you zoom out to 15 and then keep trying to zoom out, the camera bounces. I'm sure that's an easy fix, though, I just haven't put the time in yet.






                            share|improve this answer












                            This is perfect. The only change I made is to add a script to the Camera Orbit:



                            public class FollowPlayer : MonoBehaviour 

                            public GameObject player;
                            private Vector3 playerPos;

                            // Update is called once per frame
                            void Update ()
                            if (this.transform.localScale.x <= 1)

                            this.transform.localScale = new Vector3(1, 1, 1);


                            if (this.transform.localScale.x >= 15)

                            this.transform.localScale = new Vector3(15, 15, 15);


                            playerPos = player.transform.position;
                            this.transform.position = playerPos;




                            Then attach your "player" object to the Input Control, and the input control will go where ever the player does, allowing you to track the player, as well as rotate and mouse wheel zoom. Fancy.



                            The localScale if statements means you can only zoom in and out so far.



                            The only problem with this script now is that if you zoom out to 15 and then keep trying to zoom out, the camera bounces. I'm sure that's an easy fix, though, I just haven't put the time in yet.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 18 at 1:13









                            wotgeek

                            1




                            1





















                                0














                                You don't need the CameraController at all, just set the camera's z rotation to -90.






                                share|improve this answer
















                                • 1




                                  (This post does not seem to provide a quality answer to the question. Please either edit your answer and improve it, or just post it as a comment to the question.)
                                  – sɐunıɔןɐqɐp
                                  Oct 3 at 9:26















                                0














                                You don't need the CameraController at all, just set the camera's z rotation to -90.






                                share|improve this answer
















                                • 1




                                  (This post does not seem to provide a quality answer to the question. Please either edit your answer and improve it, or just post it as a comment to the question.)
                                  – sɐunıɔןɐqɐp
                                  Oct 3 at 9:26













                                0












                                0








                                0






                                You don't need the CameraController at all, just set the camera's z rotation to -90.






                                share|improve this answer












                                You don't need the CameraController at all, just set the camera's z rotation to -90.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 3 at 9:06









                                Julian Heinze

                                1




                                1







                                • 1




                                  (This post does not seem to provide a quality answer to the question. Please either edit your answer and improve it, or just post it as a comment to the question.)
                                  – sɐunıɔןɐqɐp
                                  Oct 3 at 9:26












                                • 1




                                  (This post does not seem to provide a quality answer to the question. Please either edit your answer and improve it, or just post it as a comment to the question.)
                                  – sɐunıɔןɐqɐp
                                  Oct 3 at 9:26







                                1




                                1




                                (This post does not seem to provide a quality answer to the question. Please either edit your answer and improve it, or just post it as a comment to the question.)
                                – sɐunıɔןɐqɐp
                                Oct 3 at 9:26




                                (This post does not seem to provide a quality answer to the question. Please either edit your answer and improve it, or just post it as a comment to the question.)
                                – sɐunıɔןɐqɐp
                                Oct 3 at 9:26

















                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34117591%2fc-sharp-with-unity-3d-how-do-i-make-a-camera-move-around-an-object-when-user-mo%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Use pre created SQLite database for Android project in kotlin

                                Darth Vader #20

                                Ondo