patterncsharpMinor
Player Rotation script
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.
should be renamed to:
Names should relate to what they are
Enum members should be PascalCase:
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.
Try not to abbreviate names, characters cost you nothing.
float distFromCamerashould be renamed to:
float distanceFromCameraNames 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 distFromCamerafloat distanceFromCameraenum StateOfRotation
{
Waiting,
Rotating
}Context
StackExchange Code Review Q#96443, answer score: 6
Revisions (0)
No revisions yet.