Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by ChrisJoosten · Mar 18, 2014 at 11:45 AM · c#cameraeditorcamera-movementingame

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);
 
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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! :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ChrisJoosten · Mar 18, 2014 at 12:51 PM 0
Share

Nice, this is pretty close to what I'm looking for! :)

avatar image Calum-McManus · Mar 18, 2014 at 12:51 PM 1
Share

Glad I could help! It has come in handy for me before :)

avatar image Obsdark · Aug 19, 2017 at 09:02 PM 1
Share

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.

avatar image
5

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);
         }
     }



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lenaely · Jul 04, 2019 at 08:07 AM 0
Share

THAN$$anonymous$$ YOU! I have been struggling for 2 days.

avatar image
3

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);
             }
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robrtoprz · Apr 17, 2020 at 06:35 PM 0
Share

Thank you! this works very well:)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the difference between gameobject movement speed by velocity and calculating the speed with deltatime and magnitude ? 1 Answer

Multiple Cars not working 1 Answer

How do I program camera movement 2 Answers

Move camera on Z axis instead of Y? 0 Answers

My camera won't move properly. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges