camera movement on a sphere
Hi All,
I would like to have some second pair of eyes on my script (below). First I'm very new to game dev and unity as well so forgive me my mistakes :). Straight to the piont ... what I want to achieve in this script is that that camera will move over the sphere surface directet to 'some object' when right button of the mouse is pressed (and mouse moved (x,y)). My problem here is that that behaviour of y coordinate is not what I would expect (x is very fine) ... for example camera will 'shift object' view ... please test this sript on an object and let me know what I'm missing.
Thank you
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class cameraMovement : MonoBehaviour {
/*!!!!this script should take a knonw coordinates x,y,z for camera direction as point 0,0,0 - so camera should be always directed to point 0,0,0 (rotation target object)!!!!!/?*/
private GameObject mainCamera;
private Vector3 startPos;
private float r;
private float tetha;
private float phi;
private float x, y, z;
public Transform target;
// Use this for initialization
void Start () {
//getting camera object
mainCamera = GameObject.Find("Camera");
startPos = mainCamera.transform.position;
//calcutation of start position variablas - radius and 2 angels tetha and phi - sphere coordinate system
r = Mathf.Sqrt(startPos.x * startPos.x + startPos.y * startPos.y + startPos.z * startPos.z);
//as c# documentation says an angles are measured in radians (do not cofuse with degrees).
tetha = Mathf.Acos(startPos.y / r);
phi = Mathf.Atan(startPos.x / startPos.z);
// checking if angles are in right place (check on wiki sphere coordinate system)
tetha = TethaRangeCheck(tetha);
phi = PhiRangeCheck(phi);
//calculating point on sphere - camera position
coordinationCalculation(phi, tetha);
//some debbuging parameters
Debug.Log("Camera starting variablas x,y,z" + startPos.x + " " + " " + startPos.y + " " + startPos.z);
Debug.Log("Camera starting variablas r,tetha,phi"+r+" " + " " + tetha + " " + phi);
Debug.Log("Calculated coordinates x,y,z " + x + " " + " " + y + " " + z);
}
// Update is called once per frame
void Update () {
//when right mouse button is pressed and hold
if (Input.GetMouseButton(1))
{
//mouse movement to camera movemnt
if (Input.GetAxis("Mouse X") < 0)
{
phi = phi + 0.1F;
phi = PhiRangeCheck(phi);
coordinationCalculation(phi, tetha);
}
if (Input.GetAxis("Mouse X") > 0)
{
phi = phi - 0.1F;
phi = PhiRangeCheck(phi);
coordinationCalculation(phi, tetha);
}
if (Input.GetAxis("Mouse Y") < 0)
{
tetha = tetha - 0.1F;
tetha = TethaRangeCheck(tetha);
coordinationCalculation(phi, tetha);
}
if (Input.GetAxis("Mouse Y") > 0)
{
tetha = tetha + 0.1F;
tetha = TethaRangeCheck(tetha);
coordinationCalculation(phi, tetha);
}
//debbuging parameters
Debug.Log("Current calculated coordinates x,y,z " + x + " " + " " + y + " " + z);
}
//passing position
ChangePosition(x, y, z);
//make camera look (rotate) at targer object (cameraFocus)
mainCamera.transform.LookAt(target);
}
private void ChangePosition(float xX, float yY, float zZ)
{
mainCamera.transform.position = new Vector3(xX, yY, zZ);
}
//sphere coordinate(x,y,z) calculation
private void coordinationCalculation(float pi, float teth)
{
x = r * Mathf.Sin(teth) * Mathf.Cos(pi);
z = r * Mathf.Sin(teth) * Mathf.Sin(pi);
y = r * Mathf.Cos(teth);
}
//making sure that angle is in right place
public float TethaRangeCheck(float number)
{
if (number > 3.2F)
{
number = 0F;
}
if (number < 0F)
{
number = 3.2F;
}
return number;
}
//making sure that angle is in right place
public float PhiRangeCheck(float number)
{
if (number >= 6.2F)
{
number = 0F;
}
if (number < 0F)
{
number = 6.2F;
}
return number ;
}
}
Your answer
Follow this Question
Related Questions
Issue with getting the mouse position in 3D and moving a gameobject on the y & z axis 0 Answers
Code Optimization 1 Answer
Npc spawning outside the camera view and sliding down within the camera view 0 Answers
My camera is supposed to move on the X and Y axis, but it also moves on the Z axis 2 Answers