Hi! I’m writing a C# Script to move camera. For changing its coordinate value, firstly I change the components values of a Vector3, that I named “posizione”, then I pass this vector3 to transform.position. The X and Z coordinates are updated, but Y doesn’t update. I don’t fixed this value in another part of the scipt and in others scripts. It is so strange! I will be very greatfull if someone can help me.
You can move camera around the player y axis by pressing “A” and “s”, and pressing “W” and “Z” you can move the camera up and down. When camera moves up and down, his x angle change so you can always see the player. Here you are a picture that can help you to understand camera movements (“gio” is an object where I storage the player object):
Here you are the entire camera movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCamera : MonoBehaviour {
float x_camera;
float y_camera;
float z_camera;
float x_angle;
float y_angle;
float z_angle;
float x_siny;
float z_cosy;
float y_sinx;
float distanza;
Giocatore gio;
GameObject go;
Vector3 posizione;
Vector3 direzione;
Vector3 angolazione;
// Use this for initialization
void Start () {
gio = gameObject.AddComponent<Giocatore>();
posizione = new Vector3();
go = new GameObject();
direzione = new Vector3();
distanza = 5f;
// get player position and angles
// find the player object and put it into an GameObject object
go = GameObject.FindWithTag("giocatore");
// now, convert "go" in an "Giocatore" object simply putting it into a "Giocatore" object
// "Giocatore" is the class of the player object
gio = go.gameObject.GetComponent<Giocatore>();
//inizialization of the camera angles and others variables:
angolazione = new Vector3();
x_angle = 0f;
y_angle = 0f;
z_angle = 0f;
x_siny = 0f;
z_cosy = 0f;
y_sinx = 0f;
}
// Update is called once per frame
void Update () {
// this function allow to the camera to follow the player and stay at a fixed distance from it
rincorri();
// this function allow us to move the camera around the player
Muovi();
}
// FUNZIONE RINCORRI GIOCATORE:
void rincorri() {
// let's calculate x_siny, z_cosy and y_sinx, multiplying this values for "distanza" (the fixed distance of the camera from the player)
// we can find the distance of the camera from the player in terms of x, z and y coordinates
x_siny = -Mathf.Sin(grad_to_rad(gio.angolazione.y + y_angle));
z_cosy = -Mathf.Cos(grad_to_rad(gio.angolazione.y + y_angle));
y_sinx = Mathf.Sin(grad_to_rad(x_angle));
// when we raise the camera, it roll on its x axis
// now let's define the camera position. "posizione" is a Vector3 where we storage the new values of the camera coordinates
posizione.x = gio.transform.position.x + distanza*Mathf.Cos(grad_to_rad(x_angle))*(x_siny);
posizione.y = gio.transform.position.y + distanza*(y_sinx);
posizione.z = gio.transform.position.z + distanza*Mathf.Cos(grad_to_rad(x_angle))*(z_cosy);
// ---------> HERE THE PROBLEM <--------
// now we pass the vector3 "posizione" to the vector3 "transform.position".
// In this way we update the camera position, but y coordinate not. I debug "posizione.y" and his value is right, but the camera stay
// at the same y coordinate of the player.
this.transform.position = posizione;
Debug.Log("posizione telecamera: " + transform.position);
}
void Muovi()
{
if(Input.GetKey(KeyCode.A))
{
y_angle = y_angle + accel(4f,4f);
transform.Rotate(0,accel(4f,4f),0);
}
else if(Input.GetKey(KeyCode.S))
{
y_angle = y_angle + accel(4f,-4f);
transform.Rotate(0,accel(4f,-4f),0);
}
else
{y_angle = y_angle + accel(5f,0f);}
if((transform.rotation.x >= 0 && transform.rotation.x <= 90) || (transform.rotation.x < 0 && transform.rotation.x >= -90))
{
if(Input.GetKey(KeyCode.W))
{x_angle = x_angle + 4f;}
else if(Input.GetKey(KeyCode.Z))
{x_angle = x_angle - 4f;}
}
if(x_angle > 90)
{x_angle = 90;}
if(x_angle < -90)
{x_angle = -90;}
// now we update the angles values
angolazione.x = x_angle;
angolazione.y = gio.transform.eulerAngles.y + y_angle;
angolazione.z = z_angle;
transform.eulerAngles = angolazione;
}
// this function convert angles from degrees to radiant
float grad_to_rad(float angolo)
{return (angolo * Mathf.PI)/180;}
// this function put a limit to the growing of a value
float y=0, x=0;
float smooth(float pendenza, float max_value)
{
//decellerazione
if(y > max_value)
{
y = pendenza*x;
x = x - 0.01f;
}
else if(y < max_value) // acceleration
{
y = pendenza*x;
x = x + 0.01f;
if(y >= max_value)
{y = max_value;}
}
if(Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.S))
{x = x/2;}
return y;
}
// this function put a limit to the growing of a value
float y_value=0, x_value=0;
bool can;
bool mangia; // quando è "true" si attiva la funzione crescita
float accel(float pendenza, float max_value)
{
//decellerazione
if(y_value > max_value)
{
y_value = pendenza*x_value;
x_value = x_value - 0.01f;
}
else if(y_value < max_value) // accelerazione
{
y_value = pendenza*x_value;
x_value = x_value + 0.01f;
if(y_value >= max_value)
{y_value = max_value;}
}
return y_value;
}
}