I am new to programming and am following a Tutorial so this code is not perfect. I wanted to expand on the movement to make it more interactive and fun but wonder if there is a more effective or better way of doing the movement?
I have 2 Scripts one to rotate the camera around the Focal Point with the mouse on the x Axis.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class RotateCamera : MonoBehaviour
{
[SerializeField] private float rotationSpeed = 20.0f;
[SerializeField] private float mouseSense = 1.00f;
// Start is called before the first frame update
void Awake()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
Rotate();
}
public void Rotate()
{
float horizontalInput = Input.GetAxis("Mouse X");
transform.Rotate(Vector3.up, horizontalInput * rotationSpeed * mouseSense * Time.deltaTime);
}
}
And then there is this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private float playerSpeed;
private Rigidbody playerRB;
private GameObject focalPoint;
// Start is called before the first frame update
void Start()
{
// playerRB gets the Rigigbody Component from the Player GameObject
playerRB = GetComponentInChildren<Rigidbody>();
focalPoint = GameObject.Find("Focal Point");
}
// Update is called once per frame
void Update()
{
// Get Inputs
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// Move Player
playerRB.AddForce(focalPoint.transform.forward * verticalInput * playerSpeed);
playerRB.AddForce(focalPoint.transform.right * horizontalInput * playerSpeed);
}
}