Komak57

isGrounded bug

Jul 5th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Movement : MonoBehaviour {
  5.    
  6.     public Vector2 MouseSensitivity = new Vector2(2, 2);
  7.     public bool Invert = false;
  8.     public float MoveSpeed = 10f;
  9.     public float gravity = 9.8f;
  10.     public float mass = 2;
  11.     public float JumpPower = 10f;
  12.     public bool IsGrounded = false;
  13.     public float DistanceFromGround = 0f;
  14.     float pitch, yaw;
  15.     Vector3 movement;
  16.     Vector3 velocity = Vector3.zero;
  17.     public float fallspeed = 0;
  18.     CharacterController plyr;
  19.     Respawner respawner;
  20.  
  21.     void Start () {
  22.         plyr = GetComponent<CharacterController>();
  23.         respawner = GetComponent<Respawner>();
  24.         respawner.SetPosition(transform.position);
  25.     }
  26.    
  27.     // Update is called once per frame
  28.     void Update () {
  29.         SetMoveDirection();
  30.         IsGrounded = plyr.isGrounded;
  31.         DistanceFromGround = GetGroundDistance();
  32.         if (plyr.isGrounded) {
  33.             // grounded
  34.             if (GetSlope() <= 45) {// can walk on slope
  35.                 if (fallspeed < 0) {
  36.                     //Debug.Log("Hit ground with velocity of "+fallspeed);
  37.                     fallspeed = 0;
  38.                 }
  39.                 // jumping, or walking?
  40.                 if (Input.GetButtonDown("Jump")) {
  41.                     velocity = plyr.velocity;
  42.                     fallspeed = JumpPower;
  43.                 } else {
  44.                     velocity = movement;
  45.                 }
  46.             } else {// is too steep
  47.                 AddGravity();
  48.                 velocity = new Vector3(0,fallspeed,0)+(movement*0.3f);//velocity += (movement*0.5f);
  49.             }
  50.         } else {
  51.             AddGravity();
  52.             // airborn, 50% movement adjustment
  53.             velocity += (movement*0.5f);
  54.             if (velocity.magnitude > MoveSpeed)
  55.                 velocity = Vector3.ClampMagnitude(velocity, MoveSpeed);
  56.         }
  57.         plyr.Move((AdjustSlope(velocity))*Time.deltaTime);
  58.         plyr.Move(new Vector3(0, fallspeed, 0)*Time.deltaTime);
  59.         HandleCamera();
  60.     }
  61.     float GetGroundDistance() {
  62.         RaycastHit hit;
  63.         if (Physics.Raycast(transform.position, Vector3.down, out hit)) {
  64.             //return hit.normal;
  65.             return hit.distance;
  66.         } else {
  67.             // over abyss, return flat terrain
  68.             return -1f;
  69.         }
  70.     }
  71.     float GetSlope() {
  72.         RaycastHit hit;
  73.         if (Physics.Raycast(transform.position, Vector3.down, out hit)) {
  74.             //return hit.normal;
  75.             float angle = ToDegree(Mathf.Acos(Mathf.Clamp(hit.normal.y, -1f, 1f)));
  76.             return angle;
  77.         } else {
  78.             // over abyss, return flat terrain
  79.             return 0f;
  80.         }
  81.     }
  82.     Vector3 AdjustSlope(Vector3 direction) {
  83.         RaycastHit hit;
  84.         if (Physics.Raycast(transform.position, Vector3.down, out hit)) {
  85.             Vector3 tmp = Vector3.Cross(hit.normal, direction);
  86.             return Vector3.Cross(tmp, hit.normal);
  87.         } else {
  88.             //over abyss
  89.             Vector3 tmp = Vector3.Cross(transform.up, direction);
  90.             return Vector3.Cross(tmp, transform.up);
  91.         }
  92.     }
  93.    
  94.     float ToDegree(float radians)
  95.     {
  96.         return (radians * 180.0f / 3.14f);
  97.     }
  98.    
  99.     float ToRadian(float angle)
  100.     {
  101.         return 3.14f * angle / 180.0f;
  102.     }
  103.    
  104.     void HandleCamera() {
  105.         float oldyaw = yaw;
  106.         yaw += Input.GetAxis("Mouse X") * MouseSensitivity.x;
  107.         transform.Rotate(0, yaw-oldyaw, 0); // local rotation
  108.         float oldpitch = pitch;
  109.         if (Invert)
  110.             pitch += Input.GetAxis("Mouse Y") * MouseSensitivity.y;
  111.         else
  112.             pitch += Input.GetAxis("Mouse Y") * -MouseSensitivity.y;
  113.         pitch = Mathf.Clamp(pitch, -90, 90);
  114.         Camera.main.transform.Rotate(pitch-oldpitch,0,0);
  115.     }
  116.     void SetMoveDirection() {
  117.             // Get desired direction with accelleration modifiers
  118.             Vector3 input = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
  119.             // Direction Translators
  120.             Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
  121.             forward.y = 0; forward = forward.normalized;
  122.             Vector3 right = new Vector3(forward.z, 0, -forward.x);
  123.             // calculate horizontal acceleration
  124.             movement = (input.z * right + input.x * forward);
  125.            
  126.             movement = movement * MoveSpeed;
  127.             // clamp only when too high
  128.             if (movement.magnitude > MoveSpeed)
  129.                 movement = Vector3.ClampMagnitude(movement, MoveSpeed);
  130.     }
  131.     void AddGravity() {
  132.         fallspeed -= (gravity*mass)*Time.deltaTime;
  133.         //plyr.Move(new Vector3(0, fallspeed, 0));
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment