Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by 9NICK6 · Sep 07, 2018 at 08:30 AM · positionvariableupdatingy

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):alt text

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

posizione-telecamera.png (166.7 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Sep 07, 2018 at 09:21 AM 0
Share

This is not a solution, just a $$anonymous$$or 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);
avatar image 9NICK6 · Sep 07, 2018 at 11:06 AM 0
Share

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

avatar image TreyH · Sep 07, 2018 at 11:57 AM 0
Share

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.

avatar image Harinezumi TreyH · Sep 08, 2018 at 07:03 AM 0
Share

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.

avatar image TreyH Harinezumi · Sep 08, 2018 at 08:34 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 9NICK6 · Sep 07, 2018 at 04:08 PM 0
Share

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"

avatar image misher 9NICK6 · Sep 10, 2018 at 06:52 AM 2
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create a GUI Text when reaching a point 1 Answer

Adding a value to transform.position.y runs very untrusted 0 Answers

X and Y positioning(buttons). How do they work and why does building the game change their positions? 0 Answers

How to set the mouse x and/or z position as a variable 1 Answer

How can I store each axis of a transform's rotation as a separate variable? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges