Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Movement : MonoBehaviour {
- public Vector2 MouseSensitivity = new Vector2(2, 2);
- public bool Invert = false;
- public float MoveSpeed = 10f;
- public float gravity = 9.8f;
- public float mass = 2;
- public float JumpPower = 10f;
- public bool IsGrounded = false;
- public float DistanceFromGround = 0f;
- float pitch, yaw;
- Vector3 movement;
- Vector3 velocity = Vector3.zero;
- public float fallspeed = 0;
- CharacterController plyr;
- Respawner respawner;
- void Start () {
- plyr = GetComponent<CharacterController>();
- respawner = GetComponent<Respawner>();
- respawner.SetPosition(transform.position);
- }
- // Update is called once per frame
- void Update () {
- SetMoveDirection();
- IsGrounded = plyr.isGrounded;
- DistanceFromGround = GetGroundDistance();
- if (plyr.isGrounded) {
- // grounded
- if (GetSlope() <= 45) {// can walk on slope
- if (fallspeed < 0) {
- //Debug.Log("Hit ground with velocity of "+fallspeed);
- fallspeed = 0;
- }
- // jumping, or walking?
- if (Input.GetButtonDown("Jump")) {
- velocity = plyr.velocity;
- fallspeed = JumpPower;
- } else {
- velocity = movement;
- }
- } else {// is too steep
- AddGravity();
- velocity = new Vector3(0,fallspeed,0)+(movement*0.3f);//velocity += (movement*0.5f);
- }
- } else {
- AddGravity();
- // airborn, 50% movement adjustment
- velocity += (movement*0.5f);
- if (velocity.magnitude > MoveSpeed)
- velocity = Vector3.ClampMagnitude(velocity, MoveSpeed);
- }
- plyr.Move((AdjustSlope(velocity))*Time.deltaTime);
- plyr.Move(new Vector3(0, fallspeed, 0)*Time.deltaTime);
- HandleCamera();
- }
- float GetGroundDistance() {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, Vector3.down, out hit)) {
- //return hit.normal;
- return hit.distance;
- } else {
- // over abyss, return flat terrain
- return -1f;
- }
- }
- float GetSlope() {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, Vector3.down, out hit)) {
- //return hit.normal;
- float angle = ToDegree(Mathf.Acos(Mathf.Clamp(hit.normal.y, -1f, 1f)));
- return angle;
- } else {
- // over abyss, return flat terrain
- return 0f;
- }
- }
- Vector3 AdjustSlope(Vector3 direction) {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, Vector3.down, out hit)) {
- Vector3 tmp = Vector3.Cross(hit.normal, direction);
- return Vector3.Cross(tmp, hit.normal);
- } else {
- //over abyss
- Vector3 tmp = Vector3.Cross(transform.up, direction);
- return Vector3.Cross(tmp, transform.up);
- }
- }
- float ToDegree(float radians)
- {
- return (radians * 180.0f / 3.14f);
- }
- float ToRadian(float angle)
- {
- return 3.14f * angle / 180.0f;
- }
- void HandleCamera() {
- float oldyaw = yaw;
- yaw += Input.GetAxis("Mouse X") * MouseSensitivity.x;
- transform.Rotate(0, yaw-oldyaw, 0); // local rotation
- float oldpitch = pitch;
- if (Invert)
- pitch += Input.GetAxis("Mouse Y") * MouseSensitivity.y;
- else
- pitch += Input.GetAxis("Mouse Y") * -MouseSensitivity.y;
- pitch = Mathf.Clamp(pitch, -90, 90);
- Camera.main.transform.Rotate(pitch-oldpitch,0,0);
- }
- void SetMoveDirection() {
- // Get desired direction with accelleration modifiers
- Vector3 input = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
- // Direction Translators
- Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
- forward.y = 0; forward = forward.normalized;
- Vector3 right = new Vector3(forward.z, 0, -forward.x);
- // calculate horizontal acceleration
- movement = (input.z * right + input.x * forward);
- movement = movement * MoveSpeed;
- // clamp only when too high
- if (movement.magnitude > MoveSpeed)
- movement = Vector3.ClampMagnitude(movement, MoveSpeed);
- }
- void AddGravity() {
- fallspeed -= (gravity*mass)*Time.deltaTime;
- //plyr.Move(new Vector3(0, fallspeed, 0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment