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 JDeveloper · May 01, 2013 at 12:36 AM · cameraquaternionrotatearound

Rotating Camera Around Central Point Using Arrow Keys

I am trying to make a script to rotate the camera around a central point using the arrow keys. The script currently looks like this: using UnityEngine; using System.Collections; public class NewCameraControl : MonoBehaviour { protected float fDistance; protected float fSpeed; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.RightArrow)) { orbitTower(false); } if (Input.GetKey(KeyCode.LeftArrow)) { orbitTower(true); } if (Input.GetKey(KeyCode.UpArrow)) { moveInOrOut(false); } if (Input.GetKey(KeyCode.DownArrow)) { moveInOrOut(true); } } protected void orbitTower(bool bLeft) { float step = fSpeed * Time.deltaTime; float fOrbitCircumfrance = 2F*fDistance*Mathf.PI; float fDistanceDegrees = (fSpeed / fOrbitCircumfrance) * 360; float fDistanceRadians = (fSpeed / fOrbitCircumfrance) * 2*Mathf.PI; if (bLeft) { transform.RotateAround(Tower.t().transform.position, Vector3.up, -fDistanceRadians); } else { transform.RotateAround(Tower.t().transform.position, Vector3.up, fDistanceRadians); } } protected void moveInOrOut(bool bOut) { if (bOut) { transform.Translate(0,0,-fSpeed, Space.Self); } transform.Translate(0,0,fSpeed, Space.Self); } }

Sorry that the code is in C#, I know UnityScript is more popular. The problem is that when I press the right and left arrow keys to orbit the camera (I'm not working on the in/out functionality right now), I get weird errors that I do not understand:Error

Does anyone know what's wrong and how I can fix it? Thanks!

temp.jpg (225.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by prototype7 · May 01, 2013 at 02:39 AM

Maybe because you are multiplying with undefine float fDistance Try to set the value first and i add "else" to your MoveInOrOut

 protected float fDistance = 1;
     protected float fSpeed = 1;
     public GameObject Tower;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.RightArrow)) OrbitTower(false);
         if (Input.GetKey(KeyCode.LeftArrow)) OrbitTower(true);
         if (Input.GetKey(KeyCode.UpArrow)) MoveInOrOut(false);
         if (Input.GetKey(KeyCode.DownArrow)) MoveInOrOut(true);
     }
 
     protected void OrbitTower(bool bLeft)
     {
         float step = fSpeed * Time.deltaTime;
         float fOrbitCircumfrance = 2F * fDistance * Mathf.PI;
         float fDistanceDegrees = (fSpeed / fOrbitCircumfrance) * 360;
         float fDistanceRadians = (fSpeed / fOrbitCircumfrance) * 2 * Mathf.PI;
         if (bLeft)
         {
             transform.RotateAround(Tower.transform.position, Vector3.up, -fDistanceRadians);
         }
         else
             transform.RotateAround(Tower.transform.position, Vector3.up, fDistanceRadians);
     }
 
     protected void MoveInOrOut(bool bOut)
     {
         if (bOut)   transform.Translate(0, 0, -fSpeed, Space.Self);
         else
             transform.Translate(0, 0, fSpeed, Space.Self);
     }
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 JDeveloper · May 01, 2013 at 11:20 PM 0
Share

Worked! Thank you so much!

avatar image prototype7 · May 01, 2013 at 11:44 PM 0
Share

Could you mark this question as answered(checklist mark in the left) so everybody can easy to read

avatar image TheCloudGamer · May 10, 2018 at 02:35 AM 0
Share

im a noob do you put this code into the camera and is is c#

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

14 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

Related Questions

Based on touch rotate camera object 0 Answers

Camera Rotation using Gyroscope: Yaw and Pitch reversed 2 Answers

TPS Rotation Camera (similar to WoW or any other mmo) 0 Answers

Lerp Third Person Camera Rotation 1 Answer

Camera rotation script logic error? 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