This site uses strictly necessary cookies. More Information

X- Home /

# y value doesn't update

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;
}
}
```

This is not a solution, just a minor recommendation: you can simplify limiting a value with $$anonymous$$athf.Clamp(). For example when you want to limit `x_angle`

between -90 and 90, you can write:

```
x_angle = $$anonymous$$athf.Clamp(x_angle, -90, 90);
```

Oh thanks! :) I will use this function, it's less complicated

Be careful with clamping angles, as that will only produce a sensible value if your 3 parameters are on the same branch.

-135 clamped between -90 and 90 will return -90.

225 clamped between those will return 90, even though it's equivalent to -135.

By same branch you mean same direction of rotation, right? You are right that this may cause issues if not thought through (and thank you for pointing it out :) ).

In this case (if I'm not mistaken), if you start from a valid angle between [-90, 90], and you increase/decrease from there due to input, the clamping will always make sense, because a positive angle will correspond to an input in the positive direction, while a negative angle to a negative direction. So the player will perceive movement in the correct direction, but limited to the respective limit for that direction.

Yeah as long as your angles here are all their unique representations along some interval that the author determines, then it'll be fine. The goofy behavior will appear once you allow non-unique, equivalent angle representations.

**Answer** by misher
·
Sep 07, 2018 at 08:50 AM

I think your code is a bit overkill for what you are trying to achieve :) Here is much simpler way (attach to the camera game object):

```
public Transform player;
public float speed = 1f;
private void Update()
{
if(Input.GetKey(KeyCode.A))
{
transform.RotateAround(player.position, Vector3.up, 1 * speed);
}
if (Input.GetKey(KeyCode.S))
{
transform.RotateAround(player.position, Vector3.up, -1 * speed);
}
if (Input.GetKey(KeyCode.W))
{
transform.RotateAround(player.position, transform.right, 1 * speed);
}
if (Input.GetKey(KeyCode.Z))
{
transform.RotateAround(player.position, transform.right, -1 * speed);
}
}
```

Yes, this is less complicated xD :) .but generally I like to make this functions by my self xD sometimes I think it's better, because, in my opinioni, this function put some limits. Firstly I have done another version of this script by using "RotateAround" function. Then I will give the possibility to change the distance of the camera from the player. This is very immediatly with this script, but with the other version with "RotateAround" I think It is not, but now this isn't the problem, I will find the solution... Now I can't understand why camera y coordinata doesn't change :) but thank you for answering! I will try again to use the "RotateAround"

Another tip is to add some empty object to your player (call it like *playerFocusTransform*) , position it in a point where you want your camera to always focus (like somewhere on torus or head) ins$$anonymous$$d of pivot of your character (which may be at the ground level). Attach another script to your camera with *transform.LookAt(playerFocusTransform)*, so you can be sure your camera is always looking to the focus point, this focal point could be even above the player in some cases, you can easily change it even during runtime simply by moving *playerFocusTransform* transform :)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.