using UnityEngine;
using System.Collections;
[RequireComponent (typeof(Rigidbody))]
[RequireComponent (typeof(BoxCollider))]
public class PlayerController : MonoBehaviour {
//Player movement variables
public float playerSpeed = 2.5f;
public float playerSpeedWalk = 1.5f;
public float playerSpeedRun = 4.5f;
public float maxVelocityChange = 10.0f;
public float jumpHeight = 1.0f;
public bool canJump = true;
private bool grounded = false;
//World variables
public float gravity = 10.0f;
public bool canPickUpItems = true;
public bool isPlayerHoldingItem = false;
//Components and GameObjects
public GameObject hands;
public GameObject itemNearPlayer;
public GameObject itemInHands;
public GameObject worldObject;
public Collider playerCollider;
//Awake
void Awake() {
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
//FixedUpdate
void FixedUpdate() {
if (grounded) {
//Get movement ready
Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = transform.TransformDirection(targetVelocity);
//Set the speed based on if the player should be walking, sprinting, or walking slowly.
if (Input.GetButton("Walk")) {
targetVelocity *= playerSpeedWalk;
}
else if (Input.GetButton("Run")) {
targetVelocity *= playerSpeedRun;
}
else {
targetVelocity *= playerSpeed;
}
//Move the player
Vector3 velocity = rigidbody.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
//Jumping
if (canJump && Input.GetButtonDown("Jump")) {
rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
//The "Use" key
if (Input.GetButtonDown("Use")) {
//See if the player needs to pick up an item
if (!isPlayerHoldingItem && canPickUpItems) {
PickUpItem(itemNearPlayer);
}
}
//Drop the held item
if (Input.GetButtonDown("Drop")) {
if (isPlayerHoldingItem) {
DropItem(itemInHands);
}
}
//Gravity
rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));
grounded = false;
}
float CalculateJumpVerticalSpeed() {
//Get speed from jumpHeight and gravity
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
void OnCollisionStay(Collision collision) {
//A lot of messy annoying code to make jumping not suck
//TODO: Clean this part up and make it work a bit better.
grounded = true;
Vector3 bottom = this.gameObject.transform.TransformDirection(-Vector3.up);
Vector3 forwardtest = this.gameObject.transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(this.gameObject.transform.position, bottom, 1)) {
canJump = true;
}
if (Physics.Raycast(this.gameObject.transform.position, forwardtest, 0.5f)) {
canJump = false;
}
else {
canJump = true;
}
}
void OnTriggerEnter(Collider trigger) {
//Set the itemNearPlayer variable so the item can be picked up
itemNearPlayer = trigger.gameObject.transform.parent.gameObject;
}
public void PickUpItem(GameObject item) {
//Pick the item up
itemInHands = item;
Physics.IgnoreCollision(playerCollider, item.gameObject.GetComponent<BoxCollider>(), true);
item.transform.Find("Pickup").gameObject.GetComponent<SphereCollider>().enabled = false;
item.transform.parent = hands.transform;
item.rigidbody.isKinematic = true;
item.transform.position = hands.transform.position;
item.transform.rotation = hands.transform.rotation;
canPickUpItems = false;
isPlayerHoldingItem = true;
}
public void DropItem(GameObject item) {
//Drop the held item
Physics.IgnoreCollision(playerCollider, item.gameObject.GetComponent<BoxCollider>(), false);
item.rigidbody.isKinematic = false;
item.transform.parent = worldObject.transform;
item.transform.Find("Pickup").gameObject.GetComponent<SphereCollider>().enabled = true;
canPickUpItems = true;
isPlayerHoldingItem = false;
}
}