- Home /
How to orbit camera around player from top-down to sidescroller view?
Hello, first timer here. I've seen a couple of posts on orbiting objects (perhaps not enough), but thought I'd rather elaborate on what I would like to do. Say I want the main camera to move in an orbiting fashion around the player from a side-scroller view, to a top-down view and vice versa, when the player (a hovering object) is at a certain height from terrain. Any ideas?
(I haven't really written anything in the cam script yet...)
Forgot to add I want it to orbit at a fixed distance when moving.
Answer by Eyeri · Jul 21, 2015 at 09:31 PM
I seem to have solved my problem. The truth is I got caught between complex spherical coordinate and orbital equations that I blinded myself from simple solution provided by Unity and the awesome Unity Answers community. Here's what I wanted to do and how I solved it:
The project is in a 3D environment, and it should alternate between a sort 3/4 top-down view and side-scroller view, depending on height and a certain in game situation. So I needed the camera to execute rotation when the player's height reached 200f off the terrain. If going up, camera should rotate to the top-view, and side-scroller when moving down below 200f height.
Its very basic, and if someone has a better way to do it, please do elaborate! I'll be grateful for your assistance. I hope this helps someone out there.
Here's how I solved it in steps:
Create a 3D Sphere object and Reset Position
Disable its Mesh Renderer
Enable 'is Trigger' in the Sphere Collider
Reset the desired camera's position
Make the camera a child of the Sphere object.
Move the camera to a desired offset from the Sphere's center (doesn't have to be within the sphere boundary)
Move the center of the sphere object to the center of the player object
Add a new script component to the Sphere object for movement.
Here's the script (it is not commented tho):
using UnityEngine;
using System.Collections;
public class CamSphereMovement : MonoBehaviour {
public Transform uTransform;
private HoverEngine hoverEngine;
private GameObject uPlayer;
public float xRot { get; private set; }
private float cameraZ;
private bool camFlip = false;
// Use this for initialization
void Start () {
uPlayer = GameObject.FindWithTag("Player");
uTransform = uPlayer.transform;
hoverEngine = uPlayer.GetComponent<HoverController>().hoverEngine;
cameraZ = transform.position.z;
}
void LateUpdate () {
transform.position = new Vector3(uTransform.position.x, uTransform.position.y, cameraZ);
if (camFlip == false) {
if (hoverEngine.hoverHeight.y > 200f && transform.localEulerAngles.x < 65f) {
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler (65f, 0f, 0f), 10f * Time.deltaTime);
} else {
camFlip = true;
}
} else {
if (hoverEngine.hoverHeight.y < 200f && transform.localEulerAngles.x > 0f) {
transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.Euler (0f, 0f, 0f), 10f * Time.deltaTime);
} else {
camFlip = false;
}
}
}
}
Your answer
Follow this Question
Related Questions
How to allow camera to complete an upside down rotation while using LookAt() 0 Answers
Camera movement according to rotation and actual position RTS 1 Answer
RTS Camera control - Rotation with MMB 0 Answers
Camera gets stuck when cursor is locked 0 Answers
How to rotate the camera with the player smoothly without the camera snapping back and forth? 2 Answers