Player Camera And Gravity
//PlayerMovement
public float PlayerSpeed = 1.9f;
//PlayerCamera
public Transform PlayerCamera;
//Animator & Gravity
public CharacterController cC;
public float gravity = -9.81f;
//Player Jumping & Valocity
public float jumpRange = 1f;
Vector3 velocity;
public float turnCalmTime = 0.1f;
float turnCalmValocity;
public Transform SurfaceCheck;
bool OnSurface;
public float surfaceDistance = 0.4f;
public LayerMask surfaceMusk;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
OnSurface = Physics.CheckSphere(SurfaceCheck.position, surfaceDistance, surfaceMusk);
if(OnSurface && velocity.y < 0)
{
velocity.y = -2f;
}
//gravity
velocity.y += gravity * Time.deltaTime;
cC.Move(velocity * Time.deltaTime);
PlayerMove();
jump();
}
void PlayerMove()
{
float Horizontal_Move = Input.GetAxisRaw("Horizontal");
float vertical_Move = Input.GetAxisRaw("Vertical");
Vector3 direction_Move = new Vector3(Horizontal_Move, 0f, vertical_Move).normalized;
if(direction_Move.magnitude >= 0.1f)
{
float target_Angle = Mathf.Atan2(direction_Move.x, direction_Move.z) * Mathf.Rad2Deg + PlayerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target_Angle,ref turnCalmValocity, turnCalmTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 direction_Cam = Quaternion.Euler(0f, target_Angle, 0f) * Vector3.forward;
cC.Move(direction_Cam.normalized * PlayerSpeed * Time.deltaTime);
}
}
void jump()
{
if(Input.GetButtonDown("Jump")&& OnSurface)
{
velocity.y = Mathf.Sqrt(jumpRange * -2 * gravity);
}
}