Make camera follow that can fly around a sphere at a constant height and speed

I need to make a script that can make my maincamera fly around a sphere at a constant height and speed. I need to write it in c# and I'm kinda new to that, so I need some help.

So far I have just wrote a script that orbits around the object, but I want to control the camera with WASD.

This is my orbit-script so far

public class CameraOrbit : MonoBehaviour { public GameObject target = null; public bool orbitY = false; void Start () { } void Update () { if (target != null) { transform.LookAt(target.transform); if(orbitY) { transform.RotateAround(target.transform.position, Vector3.up, Time.deltaTime * 10); } } } }

-------------Problems Reply------------

you have 2 options how to do this:

option #1 is the way you started

option #2: instead of rotating the camera around the sphere center, you can make the camera a child node of a node in the sphere center and then just change the euler angles of this parent node:

using UnityEngine;

public class RotateChild : MonoBehaviour
{
void Update()
{
if( Input.GetKey( KeyCode.W ) )
transform.Rotate( 1, 0, 0 );
if( Input.GetKey( KeyCode.S ) )
transform.Rotate( -1, 0, 0 );
if( Input.GetKey( KeyCode.A ) )
transform.Rotate( 0, -1, 0 );
if( Input.GetKey( KeyCode.D ) )
transform.Rotate( 0, 1, 0 );
}
}

use this on a node in sphere center. make the camera a child of this node.

Make camera follow that can fly around a sphere at a constant height and speed

the sphere with scale 5:

Make camera follow that can fly around a sphere at a constant height and speed

the parent of the camera with attached script RotateChild:

Make camera follow that can fly around a sphere at a constant height and speed

this works 100% - i tested it :)

Category:c# Views:5 Time:2018-05-09
Tags: unity3d orbit

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.114 (s). 11 q(s)