• 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
0
Question by angelpay · Jan 30, 2014 at 08:33 PM · c#camerarotateaxismousedrag

Rotate Camera on axis and X axis not working

Hey guys. I'm trying to rotate my camera by click and dragging. If I click then drag my mouse horizontally, I want the camera to rotate on the Y axis. If I click and drag vertically, I want my camera to rotate on the x axis. When I let up on the clicking, I want the rotating to stop.

I put this code in a script attached to my camera:

 void Update () {
     if (pressed) {
         if((Input.GetAxis("Horizontal")) == 1){
             transform.Rotate(0, Input.GetAxis("Horizontal")*speed*Time.deltaTime, 0,Space.World);
         }
         else if((Input.GetAxis("Vertical")) == -1){
             transform.Rotate(0, Input.GetAxis("Vertical")*speed*Time.deltaTime, 0,Space.World);
         }
     }
         if (Input.GetMouseButtonDown (1)) {
         if(pressed) {
             pressed = false;
         }
         else {
             pressed = true;
         }
     }


 
 }

However, when i build and run it, NOTHING happens. It does not rotate in any direction. Can someone help me? What am I doing wrong? Thanks. I'm pretty positive my structure is correct but perhaps my syntax is wrong? please let me know.

Comment
Add comment · Show 2
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 supernat · Jan 30, 2014 at 08:49 PM 0
Share

Your Get$$anonymous$$ouseButtonDown code is toggling the value pressed when the click occurs, so when you click and release, pressed will be true. The next time you click and release, pressed will be false (well technically, just when you click down). You probably don't want to use Space.World, you need Space.Self. what is speed here? I don't see the variable. Is it large enough? Is this script assigned to your camera?

avatar image angelpay · Feb 04, 2014 at 07:56 PM 0
Share

Speed was a public variable. I actually figured out my problem and I wasn't sensing when Get$$anonymous$$ouseButtonUp was positive. The script was assigned to my camera. It's figured out now though. Thank you! @supernat

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lastonestanding · May 21 at 01:47 PM

One thing I found out when it comes to changing the angle on the X axis it seems you have to use the transform.eulerAngles to do so. For I am trying to my Camera to Rotate on the X Axis as you zoom in and out here is my code a bit on that:

 public class CameraMovement : MonoBehaviour
 {
     float speed = 0.06f;
     float zoomSpeed = 10.0f;
     float rotateSpeed = 0.1f;
 
     float maxHeight = 65f;
     float minHeight =  3f;
 
     float XposHH = 50f;
     float XposHL = 3f;
 
 
     Vector2 p1;
     Vector2 p2;
 
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (Input.GetKey(KeyCode.LeftShift))
         {
             speed = 0.06f;
             zoomSpeed = 20.0f;
         }
         else 
         {
             speed = 0.035f;
             zoomSpeed = 10.0f;
         }
 
         float hsp = transform.position.y * speed * Input.GetAxis("Horizontal");
         float vsp = transform.position.y * speed * Input.GetAxis("Vertical");
         float scrollSp =  -zoomSpeed * Input.GetAxis("Mouse ScrollWheel");
 
 
 
         if (((transform.position.y >= maxHeight)) && (scrollSp > 0))
         {
             scrollSp = 0;
             transform.eulerAngles = new Vector3(XposHH, 0, 0);
         }
         else if ((transform.position.y <= minHeight) && (scrollSp < 0)) 
         {
             scrollSp = 0;
             transform.eulerAngles = new Vector3(XposHL, 0, 0);
         }


But I think this tutorial may be able to help you:

https://www.youtube.com/watch?v=T5C1BXWnOjQ&t=180s

Comment
Add comment · 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

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

20 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

Related Questions

Get object orientation 3 Answers

Distribute terrain in zones 3 Answers

How do I rotate a Rigidbody to a surface normal AND use camera rotation? 1 Answer

Make player face same direction as camera. 4 Answers

Moving Camera on player stop then move and rotate to separate point? 0 Answers


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