using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 6f;
public float jumpForce = 2f;
public Rigidbody rb;
// public CharacterController controller;
private Vector3 moveDirection;
// public float gravityScale;
public Transform Pivot;
public float rotateSpeed;
public GameObject playerModel;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
//controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void FixedUpdate()
{
rb.velocity = new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed, rb.velocity.y, Input.GetAxisRaw("Vertical") * moveSpeed);
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
}
float yStore = rb.velocity.y;
rb.velocity = (transform.forward * Input.GetAxisRaw("Vertical")) + (transform.right * Input.GetAxisRaw("Horizontal"));
rb.velocity = rb.velocity.normalized * moveSpeed;
rb.velocity = new Vector3(rb.velocity.x, yStore, rb.velocity.z);
/* moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0f, Input.GetAxis("Vertical") * moveSpeed); */
/* if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
controller.Move(moveDirection * Time.deltaTime); */
// Move the player in different directions based on camera look direction
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
transform.rotation = Quaternion.Euler(0f, Pivot.rotation.eulerAngles.y, 0f);
// Use this when you have a player model makes it turn smoother
// Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
//playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
}
}
private void Update()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
isGrounded = false;
}
}
private void OnCollisionStay(Collision c)
{
if (c.gameObject.tag == ("Ground"))
isGrounded = true;
}
private void OnCollisionExit(Collision c)
{
if (c.gameObject.tag == ("Ground"))
isGrounded = false;
}
}