- Home /
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:
Does anyone know what's wrong and how I can fix it? Thanks!
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);
}
Could you mark this question as answered(checklist mark in the left) so everybody can easy to read
im a noob do you put this code into the camera and is is c#
Your answer

Follow this Question
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