Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by molokolom · May 27, 2014 at 05:36 PM · camerarotationtransform

Camera rotation does not work properly

Hi, guys!

I'm trying to rotate the camera and make it move in a certain direction on KeyPressed. The problem is - it is not rotating properly. I'm giving the values of rotation of the camera the way it is shown in the Inspector by the function Transform.Rotate. But it is rotating 180 degrees all the time and only with Left and Right buttons. I think that it just takes a previous position of the camera as the reference instead of establishing a rotation parameters every time. Could you check it out, please, and suggest how I could solve it. Thank you!!

The Code:

using UnityEngine; using System.Collections;

public class Navigation : MonoBehaviour {

 bool moveCamUp = false;
 bool moveCamRght = false;
 bool moveCamDwn = false;
 bool moveCamLft = false;
 bool waitActive = false;    

 GenerateRoom updateRoom;



 
 void Start () {

     updateRoom = GameObject.Find("MainRoom").GetComponent("GenerateRoom") as GenerateRoom;

     Quaternion cameraRotation = Camera.mainCamera.transform.localRotation;

 }

 void Update () {



     //UpArrow
             if (Input.GetKeyDown (KeyCode.UpArrow)) {
             moveCamUp = true;

         Camera.mainCamera.transform.Rotate(new Vector3 (0f, 0f, 0f));
     

             }

 
             if (moveCamUp == true) {

                     Vector3 currentpos = Camera.mainCamera.transform.localPosition;


                     if (currentpos.z > 27f) {
                             Camera.mainCamera.transform.localPosition = new Vector3 (0f, 1.5f, -10f);

                             updateRoom.DestroyObjects ();
                             updateRoom.CreateObjects ();
                             updateRoom.ChangeTexture ();
                     }

                     currentpos.z += 0.06f;
                     Camera.mainCamera.transform.localPosition = currentpos;

                     if (currentpos.z <= 0.05f && currentpos.z >= -0.05f )
                         moveCamUp = false;
             }




     //DownArrow
     if (Input.GetKeyDown (KeyCode.DownArrow)) {
         moveCamDwn = true;
         Camera.mainCamera.transform.Rotate(new Vector3 (0f, 180f, 0f));

     }
     
     if (moveCamDwn == true) {
         
         Vector3 currentpos = Camera.mainCamera.transform.localPosition;
         
         
         if (currentpos.z < -27f) {
             Camera.mainCamera.transform.localPosition = new Vector3 (0f, 1.5f, 10f);
             
             updateRoom.DestroyObjects ();
             updateRoom.CreateObjects ();
             updateRoom.ChangeTexture ();
         }
         
         currentpos.z -= 0.06f;
         Camera.mainCamera.transform.localPosition = currentpos;
         
         if (currentpos.z <= 0.05f && currentpos.z >= -0.05f )
             moveCamDwn = false;
     }




            //RightArrow
         if (Input.GetKeyDown (KeyCode.RightArrow)) {
         moveCamRght = true;
         Camera.mainCamera.transform.Rotate(new Vector3 (0f, 90f, 0f));

     }
 
         if (moveCamRght == true) {
     
         Vector3 currentpos = Camera.mainCamera.transform.localPosition;
     
     
         if (currentpos.x > 27f) {
         
             Camera.mainCamera.transform.localPosition = new Vector3 (-10f, 1.5f, 0f);
         
             updateRoom.DestroyObjects ();
             updateRoom.CreateObjects ();
             updateRoom.ChangeTexture ();
         }

         currentpos.x += 0.06f;
         Camera.mainCamera.transform.localPosition = currentpos;
     
         if (currentpos.x <= 0.05f && currentpos.x >= -0.05f )
                 moveCamRght = false;
     }


     //LeftArrow
     if (Input.GetKeyDown (KeyCode.LeftArrow)) {
         moveCamLft = true;
         Camera.mainCamera.transform.Rotate(new Vector3 (0f, 270f, 0f));

     }
     
     if (moveCamLft == true) {
         
         Vector3 currentpos = Camera.mainCamera.transform.localPosition;
         
         
         if (currentpos.x < -27f) {
             Camera.mainCamera.transform.localPosition = new Vector3 (10f, 1.5f, 0f);
             
             updateRoom.DestroyObjects ();
             updateRoom.CreateObjects ();
             updateRoom.ChangeTexture ();
         }
         
         currentpos.x -= 0.06f;
         Camera.mainCamera.transform.localPosition = currentpos;
         
         if (currentpos.x <= 0.05f && currentpos.x >= -0.05f )
             moveCamLft = false;
     }
 

}

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by molokolom · May 28, 2014 at 05:44 PM

I'm not sure that it's exactly what I need, but thank you Andres!!

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
avatar image
0

Answer by Andres-Fernandez · May 28, 2014 at 09:37 AM

With Transform.Rotate you are just always applying the same rotation. You need to apply a rotation over time.

I suggest you have a look at the orbit script and try to get whatever you need from there (you basically need the rotation, you don't need the position and distance stuff). Something like:

 public float xSpeed = 240.0f;   // Set your speed here
 private float x = 0.0f;
  
 public void Start () {
    Vector3 angles = transform.eulerAngles;
    x = angles.y;
 }
 
 public void LateUpdate () {

       x -= Input.GetAxis("Horizontal") * xSpeed * Time.deltaTime;
 
       Quaternion rotation = Quaternion.Euler(0.0f, x, 0.0f);
       transform.rotation = rotation;
 }


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

21 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

Related Questions

How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer

An alternative to RotateAround for collisions 0 Answers

Issue with transform rotations and local rotations 1 Answer

Clamping cameras with different rotation 0 Answers

How to limit the angle of a camera with transform.RotateAround? 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