HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Player Rotation script

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptplayerrotation

Problem

This is my player rotation script. I just want to make sure my naming conventions are right. If there is any way to make this code more readable I'm open to suggestions. This is also my 3rd post about along the same line so please be brutal.

public class ShipRotation : MonoBehaviour
{
    [SerializeField] Canvas canvas;
    ShipManager shipControls;
    GameObject ship;
    DebugController debugController;
    Camera shortHandCamera;
    [SerializeField] GameObject mainCamera;
    float distFromCamera;

    void Start()
    {
        shortHandCamera = Camera.main;
        shipControls = GetComponent();
        debugController = canvas.GetComponent();
        ship = GameObject.Find("Player");
        distFromCamera = Mathf.Abs(mainCamera.transform.position.z);
        target = new Vector3(0f, 2f, ship.transform.position.z);
    }

    enum StateOfRotation
    {
        waiting,
        rotating
    }

    public LayerMask touchInputMask;
    Vector3 target;
    void Update()
    {
        foreach (Touch touch in Input.touches)
        {
            Ray ray = Camera.main.ScreenPointToRay(touch.position);
            RaycastHit hit;
            if (!Physics.Raycast(ray, out hit, touchInputMask)) { continue; }

            GameObject recipient = hit.transform.gameObject;
            if (recipient.tag != "Player")
            {
                target = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, ship.transform.position.z + distFromCamera));
            }
        }
        ship.transform.LookAt(target);
        debugController.ChangeText(6, target.ToString());
    }
}

Solution

A couple of quick minor points:

Try not to abbreviate names, characters cost you nothing.

float distFromCamera


should be renamed to:

float distanceFromCamera


Names should relate to what they are ShipManager shipControls is it a "Manager" or a set of controls? The type name doesn't match the variable name in my opinion. As an aside, I generally dislike ***Manager as a name (and I'm not the only one);

Enum members should be PascalCase:

enum StateOfRotation
{
    Waiting,
    Rotating
}


It is nicer to put all of your fields at the top of your file. You have some mixed in between method definitions.

I prefer explicit access modifiers on everything. e.g. private float distanceFromCamera. In Unity, with the whole public field thing, it can make the class easier to skim as well.

Code Snippets

float distFromCamera
float distanceFromCamera
enum StateOfRotation
{
    Waiting,
    Rotating
}

Context

StackExchange Code Review Q#96443, answer score: 6

Revisions (0)

No revisions yet.