- Home /
Question by
necipcik · Jul 09, 2019 at 03:37 PM ·
camerarotatearoundclamporbitclamped rotation
Rotating and orbiting an game object with a stop angle
hello there i have a game object located at the center of the scene and a camera which orbits it with arrow keys.
it works fine while i use left and and right keys to rotate around the object. it again work while i use up and down keys to go above and under the object. However i dont want the camera to go beyond and under certain angles/points.
i get the idea that i have to use clamping but i have no idea how to implement to the code i am using. here is the code i am using:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CameraRotator : MonoBehaviour
{
protected float fDistance = 1;
protected float fSpeed = 4;
public GameObject Tower;
void Update()
{
//if (_rotation)
// transform.Rotate(0, 20 * Time.deltaTime, 0);
if (Input.GetKey(KeyCode.RightArrow)) OrbitTower(false);
if (Input.GetKey(KeyCode.LeftArrow)) OrbitTower(true);
if (Input.GetKey(KeyCode.UpArrow)) MoveOver(false);
if (Input.GetKey(KeyCode.DownArrow)) MoveOver(true);
}
protected void OrbitTower(bool bLeft)
{
float step = fSpeed * Time.deltaTime;
float fOrbitCircumfrance = 2F * fDistance * Mathf.PI;
float fDistanceRadians = (fSpeed / fOrbitCircumfrance) * 4 * Mathf.PI;
if (bLeft)
{
transform.RotateAround(Tower.transform.position, Vector3.up, -fDistanceRadians);
}
else
transform.RotateAround(Tower.transform.position, Vector3.up, fDistanceRadians);
}
protected void MoveOver(bool bOver)
{
if (bOver)
transform.Rotate(0, 0, fSpeed, Space.Self);
else
transform.Rotate(0, 0, -fSpeed, Space.Self);
}
}
please help, my head literally hurts because of this. :) and here is a image to tell my story in a better way:
Comment