Scripts for Basic Movement and Rotation

I am fairly new to Unity. I have created a partial game that currently solely consists of a player, a platform, and some cube obstacles.

The player has a rigid body, capsule collider, and script component. There is also a camera object (player’s perspective in first person) with a script attached.

My biggest concern is am I approaching everything competently so far? Or am I setting myself up for future problems?

Player script:

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

public class main : MonoBehaviour
{
    public Rigidbody body;
    public CapsuleCollider collider;
    public Camera perspective;
    private const float movementSpeed = 100f;
    private const KeyCode moveForward = KeyCode.W;
    private const KeyCode moveBackward = KeyCode.S;
    private const KeyCode moveRight = KeyCode.D;
    private const KeyCode moveLeft = KeyCode.A;

    /**
     * Teleport player's perspective/camera/view to player/self/this
     */
    private void teleportView()
    {
        // force perspective/camera/view at player's head
        perspective.transform.position = new Vector3(transform.position.x, transform.position.y + collider.bounds.size.y, transform.position.z);
    }

    /**
     * Move this object's world coordinates relative to itself respective to the corresponding keyboard key presses
     */
    private void move()
    {
        Vector3 relativeForwardMovement = body.transform.forward * movementSpeed;
        Vector3 relativeBackwardMovement = Quaternion.AngleAxis(180, Vector3.up) * body.transform.forward * movementSpeed;
        Vector3 relativeRightMovement = Quaternion.AngleAxis(90, Vector3.up) * body.transform.forward * movementSpeed;
        Vector3 relativeLeftMovement = Quaternion.AngleAxis(-90, Vector3.up) * body.transform.forward * movementSpeed;

        Vector3 desiredMovement = Vector3.zero;

        if (Input.GetKey(moveForward))
            desiredMovement += relativeForwardMovement;
        if (Input.GetKey(moveBackward))
            desiredMovement += relativeBackwardMovement;
        if (Input.GetKey(moveRight))
            desiredMovement += relativeRightMovement;
        if (Input.GetKey(moveLeft))
            desiredMovement += relativeLeftMovement;

        body.MovePosition(body.transform.position + desiredMovement * Time.deltaTime);

        teleportView();
    }

    void Start()
    {
        Debug.Log("Started");
    }

    void Update()
    {
        move();
    }
}

Camera/player perspective script:

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

public class guidance : MonoBehaviour
{
    public Rigidbody playerBody;
    private float mouseSensitivity = 100f;

    /**
     * Rotate this object's view in response to the mouse movement relative to itself
     */
    private void rotateWithMouseMovement()
    {
        float xRotation = -Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
        float yRotation = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        transform.localEulerAngles += new Vector3(xRotation, yRotation, 0);

        rotatePlayerY();
    }

    /**
     * Rotate the player object's y coordinate to match this object's y rotation
     */
    private void rotatePlayerY()
    {
        playerBody.transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
    }

    // TEMP
    private void adjustSensitivity()
    {
        if (Input.GetKey(KeyCode.DownArrow))
            mouseSensitivity = Mathf.Max(0, mouseSensitivity - 1);
        if (Input.GetKey(KeyCode.UpArrow))
            mouseSensitivity = Mathf.Min(100000, mouseSensitivity + 1);
    }

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked; // cursor is invisible and anchored to the center of the screen
    }

    void Update()
    {
        rotateWithMouseMovement();
        adjustSensitivity();
    }
}

Any sort of feedback/criticism is welcome and appreciated.

First, huge props for getting it working. I’ll assume it works because obviously the first test is “does it work?” I’ll assume the answer is “Yes” for now.

Second, huge props for asking for feedback.

Here’s some feedback: you should follow typical C# guidelines for class names (capitalization), and come up with FAR better ones than you have. For instance main and guidance are useless names.

How about PlayerMovementController and PlayerRotationController?

Also, are you actually teleporting? That looks like some kind of positional update, perhaps for the camera. How about either:

  • make a separate CameraController script (handy when your player dies), OR
  • name that teleport function UpdateCameraPosition (for instance)

You call the guidance script “Camera/player perspective script:” but it has absolutely NOTHING to do with Camera. Even I can see that at a glance. Name things properly. It’s only professional.

Or else, use Cinemachine for camera stuff. It’s free from Unity.

Do you have a dog? If so, call him over and sit him down and explain to him what each part of your code does.

If any part of it doesn’t make sense, your dog will bark, then give him a treat and go fix that, then call him back when it’s better. For one, your code will get better, for two you will learn a lot, and for three, it’s quality time with your dog. (See my avatar picture).

I would also use a LOT more intermediate variables. You have some horribly hairy long lines of code in there. If I was reviewing your code for inclusion, I’d make you rewrite any line that had more than one or two dots in it.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

ALSO: never use eulerAngles directly. Just don’t. Here’s why:

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html