0
votes

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

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?

Code:

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

1 Answers

1
votes
  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.