Hello
I’ve my Main Camera and I’d like to lock its Z rotation. Here is the script:
using UnityEngine;
using System.Collections;
public class RotateCamera : MonoBehaviour
{
private float mouseX;
private float mouseY;
private float VerticalRotationMin = 0f;
private float VerticalRotationMax = 65f;
void LateUpdate ()
{
HandleMouseRotation();
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
}
public void HandleMouseRotation()
{
var easeFactor = 10f;
if(Input.GetMouseButton(1))
{
if(Input.mousePosition.x != mouseX)
{
var cameraRotationY = (Input.mousePosition.x - mouseX) * easeFactor * Time.deltaTime;
this.transform.Rotate(0, cameraRotationY, 0);
}
if(Input.mousePosition.y != mouseY)
{
GameObject MainCamera = this.gameObject;
var cameraRotationX = (Input.mousePosition.y - mouseY) * easeFactor * Time.deltaTime;
var desiredRotationX = MainCamera.transform.eulerAngles.x + cameraRotationX;
if(desiredRotationX >= VerticalRotationMin desiredRotationX <= VerticalRotationMax)
MainCamera.transform.Rotate (cameraRotationX, 0, 0);
}
}
}
}
I’d be thankful if someone told me how to do that. Thanks!
Attach a rigidibody and tick Lock Z Position
In pseudo-code:
void LateUpdate()
{
float eulerZ = transform.localEulerAngles.z;
// here is your code
Vector3 euler = transform.localEuleraAngles.z;
euler.z = euler.Z;
transform.localEulerAngles = euler;
}
Wouldn’t it make camera feel like real body, so it’ll fall down on the ground?
You can disable gravity of a rigidbody 
Damn… Yes it work’s… However i’d like my Camera to act like in picture:
easiest way to achieve that is to steal the idea of a “camera dolly” from cinematography.
You have a “dolly” gameObject which is the parent of the camera, the camera has a relative rotation to the parent “dolly” so it’s pointing at whatever angle you like in the up/down plane. You then move and rotate the dolly in the left/right forward/backwards plane (and yes i get the x, y, z all mixed up :p).
You can probably do something with the scripting but the dolly approach is something I’ve found quite intuitive.
I have no idea how to do that =)
The trick is to reconstruct the rotation of the camera each frame instead of using the previous transform.
using UnityEngine;
using System.Collections;
public class RotateCamera : MonoBehaviour
{
public Vector3 center = new Vector3(0f,0f,0f);
public float distance = 20f;
public float rotateX = 40f;
public float rotateY = 0f;
public float rotateXMin = 0f;
public float rotateXMax = 65f;
public float easeFactor = 10.0f;
private Vector3 startMousePos;
private float startRotX;
private float startRotY;
private float rotateGoalX;
private float rotateGoalY;
private bool rotating;
void Start ()
{
rotateGoalX = rotateX;
rotateGoalY = rotateY;
rotating = false;
}
void Update ()
{
if(Input.GetMouseButton (1)) {
if (!rotating) {
// Start rotating
startMousePos = Input.mousePosition;
startRotX = rotateX;
startRotY = rotateY;
rotating = true;
}
Vector3 delta = Input.mousePosition - startMousePos;
rotateGoalX = Mathf.Clamp (startRotX - delta.y, rotateXMin, rotateXMax);
rotateGoalY = startRotY + delta.x;
}
else
{
rotating = false;
}
float dt = Mathf.Min (Time.deltaTime * easeFactor, 1.0f);
rotateX += (rotateGoalX - rotateX) * dt;
rotateY += (rotateGoalY - rotateY) * dt;
}
void LateUpdate ()
{
// Reset transform
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
// Apply camera movement.
transform.Translate (center);
transform.Rotate (0f, rotateY, 0f);
transform.Rotate (rotateX, 0f, 0f);
transform.Translate (0f,0f,-distance);
}
}
Wow. I have to say - “It works!”. Thanks a lot =)
However there’s no one little problem. I can not move camera around the map and my starting position of it is 0:0:0 even if inspector positions are correct…
This is RTSCamera script:
var CamSpeed = 1.00;
var GUIsize = 25;
function Update () {
var recdown = Rect (0, 0, Screen.width, GUIsize);
var recup = Rect (0, Screen.height-GUIsize, Screen.width, GUIsize);
var recleft = Rect (0, 0, GUIsize, Screen.height);
var recright = Rect (Screen.width-GUIsize, 0, GUIsize, Screen.height);
if (recdown.Contains(Input.mousePosition)) {
if (transform.position.z > 0) {
transform.Translate(0, 0, -CamSpeed, Space.World);
}
}
if (recup.Contains(Input.mousePosition)){
if (transform.position.z < 400) {
transform.Translate(0, 0, CamSpeed, Space.World);
}
}
if (recleft.Contains(Input.mousePosition)){
if (transform.position.x > 0) {
transform.Translate(-CamSpeed, 0, 0, Space.World);
}
}
if (recright.Contains(Input.mousePosition)){
if (transform.position.x < 400) {
transform.Translate(CamSpeed, 0, 0, Space.World);
}
}
}