this is my player Controller script
using UnityEngine;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(ConfigurableJoint))]
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float speed = 5f;
[SerializeField]
private float lookSensitivity = 3f;
[SerializeField]
private float thrusterForce = 1000f;
[Header("Spring settings:")]
[SerializeField]
private JointDriveMode jointMode = JointDriveMode.Position;
[SerializeField]
private float jointSpring = 20f;
[SerializeField]
private float jointMaxForce = 40f;
// Component caching
private PlayerMotor motor;
private ConfigurableJoint joint;
private Animator animator;
void Start()
{
motor = GetComponent<PlayerMotor>();
joint = GetComponent<ConfigurableJoint>();
animator = GetComponent<Animator>();
SetJointSettings(jointSpring);
}
void Update()
{
//Calculate movement velocity as a 3D vector
float _xMov = Input.GetAxis("Horizontal");
float _zMov = Input.GetAxis("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
// Finale movement vector
Vector3 _Velocity = (_movHorizontal + _movVertical) * speed;
// Animate movement
animator.SetFloat("ForwardVelocity", _zMov);
//Apply movement
motor.Move(_Velocity);
//Calculate rotation as a 3D vector (turning around)
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
//Apply rotation
motor.Rotate(_rotation);
//Calculate camera rotation as a 3D vector (turning around)
float _xRot = Input.GetAxisRaw("Mouse Y");
float _cameraRotationX = _xRot * lookSensitivity;
//Apply camera rotation
motor.RotateCamera(_cameraRotationX);
// Calculate the thrusterforce based on player input
Vector3 _thrusterForce = Vector3.zero;
if (Input.GetButton("Jump"))
{
_thrusterForce = Vector3.up * thrusterForce;
SetJointSettings(0f);
} else
{
SetJointSettings(jointSpring);
}
}
private void SetJointSettings (float _jointSpring)
{
joint.yDrive = new JointDrive {
mode = jointMode,
positionSpring = _jointSpring,
maximumForce = jointMaxForce
};
}
}
And this is my Player Motor Script,
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour
{
[SerializeField]
private Camera cam;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private float cameraRotationX = 0f;
private float currentCameraRotationX = 0f;
private Vector3 thrusterForce = Vector3.zero;
[SerializeField]
private float cameraRotationLimit = 85f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
//Gets a movement vector
public void Move(Vector3 _velocity)
{
velocity = _velocity;
}
//Gets a rotational vector
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
//Gets a rotational vector for the camera
public void RotateCamera(float _cameraRotationX)
{
cameraRotationX = _cameraRotationX;
}
//Get a force Vector for our thrusters
public void ApplyThruster(Vector3 _thrusterForce)
{
thrusterForce = _thrusterForce;
}
// Run every physics iteration
void FixedUpdate()
{
PerformMovement();
PerformRotation();
}
//Perform movement based on velocity variable
void PerformMovement()
{
if(velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
if (thrusterForce != Vector3.zero)
{
rb.AddForce(thrusterForce * Time.fixedDeltaTime, ForceMode.Acceleration);
}
}
//Perform rotation
void PerformRotation()
{
rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
if(cam != null)
{
// Set our rotation and clamp it
currentCameraRotationX -= cameraRotationX;
currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit, cameraRotationLimit);
//Apply our rotation to the transform of our camera
cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f);
}
}
}
i am using a brackeys tutorial.