153 lines
4.0 KiB
C#
153 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class Character : MonoBehaviour
|
|
{
|
|
[Header("Movement")]
|
|
public float moveSpeed = 2f;
|
|
public float groundDrag = 1f;
|
|
|
|
private float horizontalInput, verticalInput;
|
|
private Vector3 moveDirection;
|
|
private Rigidbody rb;
|
|
|
|
public float speed = 2f;
|
|
|
|
[Header("Turning")]
|
|
public float turningRate = 30f;
|
|
public Quaternion _targetRotation = Quaternion.identity;
|
|
|
|
[Header("Keybindings")]
|
|
public KeyCode jumpKey = KeyCode.Space;
|
|
|
|
public float jumpForce;
|
|
public float jumpCooldown;
|
|
bool readyToJump = true;
|
|
|
|
[Header("Groud Check")]
|
|
public float playerHeight;
|
|
public LayerMask whatIsGround;
|
|
public bool grounded;
|
|
|
|
[Header("Hold item")]
|
|
[SerializeField] private Transform _hand;
|
|
[SerializeField] private GameObject _carriedItem;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
rb = gameObject.GetComponent<Rigidbody>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
|
|
|
|
MyInput();
|
|
SpeedControl();
|
|
|
|
transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, turningRate * Time.deltaTime);
|
|
|
|
if (grounded)
|
|
{
|
|
rb.drag = groundDrag;
|
|
}
|
|
else
|
|
{
|
|
rb.drag = 0.3f;
|
|
}
|
|
|
|
}
|
|
|
|
public void PickupItem(Pickupable itemToPickup)
|
|
{
|
|
if (_carriedItem)
|
|
{
|
|
Pickupable pickupable = _carriedItem.GetComponentInChildren<Pickupable>(true);
|
|
|
|
pickupable.gameObject.SetActive(true);
|
|
pickupable.transform.parent = null;
|
|
|
|
pickupable.transform.position = _carriedItem.transform.position;
|
|
pickupable.transform.rotation = _carriedItem.transform.rotation;
|
|
|
|
Rigidbody rigidbody = pickupable.GetComponent<Rigidbody>();
|
|
|
|
rigidbody.AddForce(Random.insideUnitSphere * 2.0f, ForceMode.Impulse);
|
|
|
|
Destroy(_carriedItem);
|
|
|
|
_carriedItem = null;
|
|
}
|
|
|
|
if (itemToPickup)
|
|
{
|
|
GameObject model = Instantiate(itemToPickup.Model, _hand);
|
|
|
|
itemToPickup.transform.parent = model.transform;
|
|
itemToPickup.gameObject.SetActive(false);
|
|
|
|
_carriedItem = model;
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
MovePlayer();
|
|
}
|
|
|
|
private void MyInput()
|
|
{
|
|
horizontalInput = Input.GetAxisRaw("Horizontal");
|
|
verticalInput = Input.GetAxisRaw("Vertical");
|
|
|
|
if(horizontalInput != 0f || verticalInput != 0f)
|
|
{
|
|
_targetRotation = Quaternion.Euler(new Vector3(0f, Mathf.Atan2(horizontalInput, verticalInput) * Mathf.Rad2Deg, 0f));
|
|
}
|
|
|
|
if (Input.GetKey(jumpKey) && readyToJump && grounded)
|
|
{
|
|
readyToJump = false;
|
|
|
|
Jump();
|
|
|
|
Invoke(nameof(ResetJump), jumpCooldown);
|
|
}
|
|
|
|
}
|
|
|
|
private void MovePlayer()
|
|
{
|
|
moveDirection = new Vector3(0, 0, verticalInput) + new Vector3(horizontalInput, 0, 0);
|
|
|
|
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
|
|
}
|
|
|
|
private void SpeedControl()
|
|
{
|
|
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
|
|
|
|
if (flatVel.magnitude > moveSpeed)
|
|
{
|
|
Vector3 limitedVel = flatVel.normalized * moveSpeed;
|
|
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
|
|
}
|
|
}
|
|
|
|
private void Jump()
|
|
{
|
|
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
|
|
|
|
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
|
|
}
|
|
|
|
private void ResetJump()
|
|
{
|
|
readyToJump = true;
|
|
}
|
|
}
|