Please help me to understand what's going on? (First person controller)












0














I am creating a first person controller in Unity. Very basic stuff, camera is a child of Player capsule. Codes work, but I need help explaining what's going on.



first person controller
*camera is child of Player in hierarchy
hierarchy http://i1170.photobucket.com/albums/r522/charlotteseverus/hierarchy.png



These are my questions:



1) In PlayerMovement, why are we translating in the Z-axis to achieve vertical movement when Unity is in Y-Up axis?



2) In CamRotation, I have no idea what is going on in Update ( ). Why are we applying horizontal movement to our Player, but then vertical movement to our Camera? Why is it not applied on the same GameObject?



3) What is mouseMove trying to achieve? Why do we use var?



4) I think we are getting a value of how much mouse has been moved, but what then does applying Vector2.Scale do to it?



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

public float speed = 5.0f;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
float mvX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
float mvZ = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(mvX, 0, mvZ);
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CamRotation : MonoBehaviour {

public float horizontal_speed = 3.0F;
public float vertical_speed = 2.0F;
GameObject character; // refers to the parent object the camera is attached to (our Player capsule)

// initialization
void Start()
{
character = this.transform.parent.gameObject;
}

// Update is called once per frame
void Update()
{
var mouseMove = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseMove = Vector2.Scale(mouseMove, new Vector2(horizontal_speed, vertical_speed));

character.transform.Rotate(0, mouseMove.x, 0); // to rotate our character horizontally
transform.Rotate(-mouseMove.y, 0, 0); // to rotate the camera vertically
}
}









share|improve this question



























    0














    I am creating a first person controller in Unity. Very basic stuff, camera is a child of Player capsule. Codes work, but I need help explaining what's going on.



    first person controller
    *camera is child of Player in hierarchy
    hierarchy http://i1170.photobucket.com/albums/r522/charlotteseverus/hierarchy.png



    These are my questions:



    1) In PlayerMovement, why are we translating in the Z-axis to achieve vertical movement when Unity is in Y-Up axis?



    2) In CamRotation, I have no idea what is going on in Update ( ). Why are we applying horizontal movement to our Player, but then vertical movement to our Camera? Why is it not applied on the same GameObject?



    3) What is mouseMove trying to achieve? Why do we use var?



    4) I think we are getting a value of how much mouse has been moved, but what then does applying Vector2.Scale do to it?



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour {

    public float speed = 5.0f;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    float mvX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    float mvZ = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    transform.Translate(mvX, 0, mvZ);
    }
    }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CamRotation : MonoBehaviour {

    public float horizontal_speed = 3.0F;
    public float vertical_speed = 2.0F;
    GameObject character; // refers to the parent object the camera is attached to (our Player capsule)

    // initialization
    void Start()
    {
    character = this.transform.parent.gameObject;
    }

    // Update is called once per frame
    void Update()
    {
    var mouseMove = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    mouseMove = Vector2.Scale(mouseMove, new Vector2(horizontal_speed, vertical_speed));

    character.transform.Rotate(0, mouseMove.x, 0); // to rotate our character horizontally
    transform.Rotate(-mouseMove.y, 0, 0); // to rotate the camera vertically
    }
    }









    share|improve this question

























      0












      0








      0


      1





      I am creating a first person controller in Unity. Very basic stuff, camera is a child of Player capsule. Codes work, but I need help explaining what's going on.



      first person controller
      *camera is child of Player in hierarchy
      hierarchy http://i1170.photobucket.com/albums/r522/charlotteseverus/hierarchy.png



      These are my questions:



      1) In PlayerMovement, why are we translating in the Z-axis to achieve vertical movement when Unity is in Y-Up axis?



      2) In CamRotation, I have no idea what is going on in Update ( ). Why are we applying horizontal movement to our Player, but then vertical movement to our Camera? Why is it not applied on the same GameObject?



      3) What is mouseMove trying to achieve? Why do we use var?



      4) I think we are getting a value of how much mouse has been moved, but what then does applying Vector2.Scale do to it?



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class PlayerMovement : MonoBehaviour {

      public float speed = 5.0f;

      // Use this for initialization
      void Start () {

      }

      // Update is called once per frame
      void Update () {
      float mvX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
      float mvZ = Input.GetAxis("Vertical") * Time.deltaTime * speed;
      transform.Translate(mvX, 0, mvZ);
      }
      }

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class CamRotation : MonoBehaviour {

      public float horizontal_speed = 3.0F;
      public float vertical_speed = 2.0F;
      GameObject character; // refers to the parent object the camera is attached to (our Player capsule)

      // initialization
      void Start()
      {
      character = this.transform.parent.gameObject;
      }

      // Update is called once per frame
      void Update()
      {
      var mouseMove = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
      mouseMove = Vector2.Scale(mouseMove, new Vector2(horizontal_speed, vertical_speed));

      character.transform.Rotate(0, mouseMove.x, 0); // to rotate our character horizontally
      transform.Rotate(-mouseMove.y, 0, 0); // to rotate the camera vertically
      }
      }









      share|improve this question













      I am creating a first person controller in Unity. Very basic stuff, camera is a child of Player capsule. Codes work, but I need help explaining what's going on.



      first person controller
      *camera is child of Player in hierarchy
      hierarchy http://i1170.photobucket.com/albums/r522/charlotteseverus/hierarchy.png



      These are my questions:



      1) In PlayerMovement, why are we translating in the Z-axis to achieve vertical movement when Unity is in Y-Up axis?



      2) In CamRotation, I have no idea what is going on in Update ( ). Why are we applying horizontal movement to our Player, but then vertical movement to our Camera? Why is it not applied on the same GameObject?



      3) What is mouseMove trying to achieve? Why do we use var?



      4) I think we are getting a value of how much mouse has been moved, but what then does applying Vector2.Scale do to it?



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class PlayerMovement : MonoBehaviour {

      public float speed = 5.0f;

      // Use this for initialization
      void Start () {

      }

      // Update is called once per frame
      void Update () {
      float mvX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
      float mvZ = Input.GetAxis("Vertical") * Time.deltaTime * speed;
      transform.Translate(mvX, 0, mvZ);
      }
      }

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class CamRotation : MonoBehaviour {

      public float horizontal_speed = 3.0F;
      public float vertical_speed = 2.0F;
      GameObject character; // refers to the parent object the camera is attached to (our Player capsule)

      // initialization
      void Start()
      {
      character = this.transform.parent.gameObject;
      }

      // Update is called once per frame
      void Update()
      {
      var mouseMove = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
      mouseMove = Vector2.Scale(mouseMove, new Vector2(horizontal_speed, vertical_speed));

      character.transform.Rotate(0, mouseMove.x, 0); // to rotate our character horizontally
      transform.Rotate(-mouseMove.y, 0, 0); // to rotate the camera vertically
      }
      }






      c# unity3d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      gremolada

      216




      216
























          1 Answer
          1






          active

          oldest

          votes


















          1















          1. XY is the plane for 2D Unity games. For 3D, you have Z-axis for height, and XY plane for positioning.


          2. Note that different components from mouseMove are being applied (.x for character and .y for the camera). This means that the movement from the character is not equal to the movement from the camera; one should be faster/slower than the other.


          3. var is a predefined C# keyword that lets the compiler figure out an appropriate type. In this case, it's the same as if you had written Vector2 as in Vector2 mouseMove = new Vector2(...);.


          4. You are scaling the value from mouseMove, by multiplying its components by predefined values in your code. That's just it.



          EDIT



          You apply .x to your character because, as you commented after the line of code, you want to move it horizontally. As for the camera, .y is being applied because you want to move it vertically.
          The negative value could be because the axis is inverted, so you make it negative so the camera has a natural movement. It's the same principle some games have in the setting, where they allow you to invert Y-axis.






          share|improve this answer























          • Thanks so much. I am understanding it better now. @krobelusmeetsyndra Can you please also explain to me in CamRotation, why we are applying rotation in y for character, and applying rotation in x for camera? What I mean is in: character.transform.Rotate(0, mouseMove.x, 0); transform.Rotate(-mouseMove.y, 0, 0); Also, why are we wanting the negative value? (-mouseMove.y) Wow sorry that this commentlooks so messy I am not sure how to format it better.
            – gremolada
            12 hours ago












          • I've edited my answer so it also covers those questions you made in the comment. And please, if my question is fit for you, accept it. =)
            – krobelusmeetsyndra
            6 hours ago












          • thank you so much :)
            – gremolada
            5 hours ago










          • Glad I could help!
            – krobelusmeetsyndra
            5 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943565%2fplease-help-me-to-understand-whats-going-on-first-person-controller%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          1. XY is the plane for 2D Unity games. For 3D, you have Z-axis for height, and XY plane for positioning.


          2. Note that different components from mouseMove are being applied (.x for character and .y for the camera). This means that the movement from the character is not equal to the movement from the camera; one should be faster/slower than the other.


          3. var is a predefined C# keyword that lets the compiler figure out an appropriate type. In this case, it's the same as if you had written Vector2 as in Vector2 mouseMove = new Vector2(...);.


          4. You are scaling the value from mouseMove, by multiplying its components by predefined values in your code. That's just it.



          EDIT



          You apply .x to your character because, as you commented after the line of code, you want to move it horizontally. As for the camera, .y is being applied because you want to move it vertically.
          The negative value could be because the axis is inverted, so you make it negative so the camera has a natural movement. It's the same principle some games have in the setting, where they allow you to invert Y-axis.






          share|improve this answer























          • Thanks so much. I am understanding it better now. @krobelusmeetsyndra Can you please also explain to me in CamRotation, why we are applying rotation in y for character, and applying rotation in x for camera? What I mean is in: character.transform.Rotate(0, mouseMove.x, 0); transform.Rotate(-mouseMove.y, 0, 0); Also, why are we wanting the negative value? (-mouseMove.y) Wow sorry that this commentlooks so messy I am not sure how to format it better.
            – gremolada
            12 hours ago












          • I've edited my answer so it also covers those questions you made in the comment. And please, if my question is fit for you, accept it. =)
            – krobelusmeetsyndra
            6 hours ago












          • thank you so much :)
            – gremolada
            5 hours ago










          • Glad I could help!
            – krobelusmeetsyndra
            5 hours ago
















          1















          1. XY is the plane for 2D Unity games. For 3D, you have Z-axis for height, and XY plane for positioning.


          2. Note that different components from mouseMove are being applied (.x for character and .y for the camera). This means that the movement from the character is not equal to the movement from the camera; one should be faster/slower than the other.


          3. var is a predefined C# keyword that lets the compiler figure out an appropriate type. In this case, it's the same as if you had written Vector2 as in Vector2 mouseMove = new Vector2(...);.


          4. You are scaling the value from mouseMove, by multiplying its components by predefined values in your code. That's just it.



          EDIT



          You apply .x to your character because, as you commented after the line of code, you want to move it horizontally. As for the camera, .y is being applied because you want to move it vertically.
          The negative value could be because the axis is inverted, so you make it negative so the camera has a natural movement. It's the same principle some games have in the setting, where they allow you to invert Y-axis.






          share|improve this answer























          • Thanks so much. I am understanding it better now. @krobelusmeetsyndra Can you please also explain to me in CamRotation, why we are applying rotation in y for character, and applying rotation in x for camera? What I mean is in: character.transform.Rotate(0, mouseMove.x, 0); transform.Rotate(-mouseMove.y, 0, 0); Also, why are we wanting the negative value? (-mouseMove.y) Wow sorry that this commentlooks so messy I am not sure how to format it better.
            – gremolada
            12 hours ago












          • I've edited my answer so it also covers those questions you made in the comment. And please, if my question is fit for you, accept it. =)
            – krobelusmeetsyndra
            6 hours ago












          • thank you so much :)
            – gremolada
            5 hours ago










          • Glad I could help!
            – krobelusmeetsyndra
            5 hours ago














          1












          1








          1







          1. XY is the plane for 2D Unity games. For 3D, you have Z-axis for height, and XY plane for positioning.


          2. Note that different components from mouseMove are being applied (.x for character and .y for the camera). This means that the movement from the character is not equal to the movement from the camera; one should be faster/slower than the other.


          3. var is a predefined C# keyword that lets the compiler figure out an appropriate type. In this case, it's the same as if you had written Vector2 as in Vector2 mouseMove = new Vector2(...);.


          4. You are scaling the value from mouseMove, by multiplying its components by predefined values in your code. That's just it.



          EDIT



          You apply .x to your character because, as you commented after the line of code, you want to move it horizontally. As for the camera, .y is being applied because you want to move it vertically.
          The negative value could be because the axis is inverted, so you make it negative so the camera has a natural movement. It's the same principle some games have in the setting, where they allow you to invert Y-axis.






          share|improve this answer















          1. XY is the plane for 2D Unity games. For 3D, you have Z-axis for height, and XY plane for positioning.


          2. Note that different components from mouseMove are being applied (.x for character and .y for the camera). This means that the movement from the character is not equal to the movement from the camera; one should be faster/slower than the other.


          3. var is a predefined C# keyword that lets the compiler figure out an appropriate type. In this case, it's the same as if you had written Vector2 as in Vector2 mouseMove = new Vector2(...);.


          4. You are scaling the value from mouseMove, by multiplying its components by predefined values in your code. That's just it.



          EDIT



          You apply .x to your character because, as you commented after the line of code, you want to move it horizontally. As for the camera, .y is being applied because you want to move it vertically.
          The negative value could be because the axis is inverted, so you make it negative so the camera has a natural movement. It's the same principle some games have in the setting, where they allow you to invert Y-axis.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 hours ago

























          answered yesterday









          krobelusmeetsyndra

          11510




          11510












          • Thanks so much. I am understanding it better now. @krobelusmeetsyndra Can you please also explain to me in CamRotation, why we are applying rotation in y for character, and applying rotation in x for camera? What I mean is in: character.transform.Rotate(0, mouseMove.x, 0); transform.Rotate(-mouseMove.y, 0, 0); Also, why are we wanting the negative value? (-mouseMove.y) Wow sorry that this commentlooks so messy I am not sure how to format it better.
            – gremolada
            12 hours ago












          • I've edited my answer so it also covers those questions you made in the comment. And please, if my question is fit for you, accept it. =)
            – krobelusmeetsyndra
            6 hours ago












          • thank you so much :)
            – gremolada
            5 hours ago










          • Glad I could help!
            – krobelusmeetsyndra
            5 hours ago


















          • Thanks so much. I am understanding it better now. @krobelusmeetsyndra Can you please also explain to me in CamRotation, why we are applying rotation in y for character, and applying rotation in x for camera? What I mean is in: character.transform.Rotate(0, mouseMove.x, 0); transform.Rotate(-mouseMove.y, 0, 0); Also, why are we wanting the negative value? (-mouseMove.y) Wow sorry that this commentlooks so messy I am not sure how to format it better.
            – gremolada
            12 hours ago












          • I've edited my answer so it also covers those questions you made in the comment. And please, if my question is fit for you, accept it. =)
            – krobelusmeetsyndra
            6 hours ago












          • thank you so much :)
            – gremolada
            5 hours ago










          • Glad I could help!
            – krobelusmeetsyndra
            5 hours ago
















          Thanks so much. I am understanding it better now. @krobelusmeetsyndra Can you please also explain to me in CamRotation, why we are applying rotation in y for character, and applying rotation in x for camera? What I mean is in: character.transform.Rotate(0, mouseMove.x, 0); transform.Rotate(-mouseMove.y, 0, 0); Also, why are we wanting the negative value? (-mouseMove.y) Wow sorry that this commentlooks so messy I am not sure how to format it better.
          – gremolada
          12 hours ago






          Thanks so much. I am understanding it better now. @krobelusmeetsyndra Can you please also explain to me in CamRotation, why we are applying rotation in y for character, and applying rotation in x for camera? What I mean is in: character.transform.Rotate(0, mouseMove.x, 0); transform.Rotate(-mouseMove.y, 0, 0); Also, why are we wanting the negative value? (-mouseMove.y) Wow sorry that this commentlooks so messy I am not sure how to format it better.
          – gremolada
          12 hours ago














          I've edited my answer so it also covers those questions you made in the comment. And please, if my question is fit for you, accept it. =)
          – krobelusmeetsyndra
          6 hours ago






          I've edited my answer so it also covers those questions you made in the comment. And please, if my question is fit for you, accept it. =)
          – krobelusmeetsyndra
          6 hours ago














          thank you so much :)
          – gremolada
          5 hours ago




          thank you so much :)
          – gremolada
          5 hours ago












          Glad I could help!
          – krobelusmeetsyndra
          5 hours ago




          Glad I could help!
          – krobelusmeetsyndra
          5 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943565%2fplease-help-me-to-understand-whats-going-on-first-person-controller%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas