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.