Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using DG.Tweening;
- public class movement_controller : MonoBehaviour {
- [HideInInspector]
- public float distanceToGround;
- public float skinThickness = 0.1f;
- public float curMovementSpeed;
- public float maxVelocity;
- public bool isGrounded = false;
- [HideInInspector]
- public bool wasGrounded = false;
- public bool touchDown = false;
- public bool isJumping = false;
- public bool isMoving = false;
- public bool canMove = true;
- public Vector3 velocity;
- public Rigidbody rb;
- public Character_Animation ca;
- public CapsuleCollider collider;
- public Transform currentGround;
- public float lastRotation;
- public void Start()
- {
- collider = this.GetComponent<CapsuleCollider>();
- rb = this.GetComponent<Rigidbody>();
- distanceToGround = this.GetComponent<Collider>().bounds.extents.y;
- }
- public void Update()
- {
- velocity = rb.velocity;
- Vector3 velo = rb.velocity;
- velo.x = Mathf.Clamp(velo.x, -maxVelocity, maxVelocity);
- velo.z = Mathf.Clamp(velo.z, -maxVelocity, maxVelocity);
- rb.velocity = velo;
- wasGrounded = isGrounded;
- Ray groundedRay = new Ray(transform.position, -Vector3.up);
- RaycastHit groundedHit;
- isGrounded = Physics.Raycast(groundedRay, out groundedHit, distanceToGround + skinThickness);
- if (groundedHit.transform != null && groundedHit.transform.gameObject.layer == LayerMask.NameToLayer("ground"))
- currentGround = groundedHit.transform;
- else
- currentGround = null;
- }
- public void AddForce(Vector3 _force)
- {
- rb.AddForce(_force);
- }
- public void SetRotation(float _rot)
- {
- _rot = Mathf.Round(_rot / 45) * 45;
- lastRotation = _rot;
- if (canMove)
- rb.rotation = (Quaternion.Euler(new Vector3(0, _rot, 0)));
- else
- {
- Vector3 rotation = this.transform.rotation.eulerAngles;
- rotation.y = _rot;
- this.transform.rotation = Quaternion.Euler(rotation);
- }
- }
- public void SetCanMove(bool _canMove)
- {
- canMove = _canMove;
- rb.isKinematic = !_canMove;
- }
- public void SetPosition(Vector3 _pos, float _rot, float _time)
- {
- StartCoroutine(SetPositionLoop(_pos, _rot, _time));
- }
- public IEnumerator SetPositionLoop(Vector3 _pos, float _rot, float _time)
- {
- isMoving = true;
- this.transform.DOMove(_pos, _time);
- SetRotation(GetAngleBetweenPoints(this.transform.position, _pos));
- yield return new WaitForSeconds(_time);
- SetRotation(_rot);
- isMoving = false;
- }
- public float GetAngleBetweenPoints(Vector3 _pos1, Vector3 _pos2)
- {
- float deltaZ = _pos1.z - _pos2.z;
- float deltaX = _pos1.x - _pos2.x;
- return (float)Mathf.Atan2(deltaZ, deltaX) * Mathf.Rad2Deg;
- }
- public void TouchDown(float _time = 0.25f)
- {
- StartCoroutine(TouchDownLoop(_time));
- }
- public IEnumerator TouchDownLoop(float _time)
- {
- touchDown = true;
- yield return new WaitForSeconds(_time);
- touchDown = false;
- }
- /// <summary>
- /// Sets the collider of the character.
- /// </summary>
- /// <param name="_collider"></param>
- public void SetCollider(PhysicMaterial _collider)
- {
- collider.material = _collider;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement