viewing paste Unknown #20930 | C#

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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;
    }
}
 
Viewed 1069 times, submitted by IntelligenceExalted.