Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Movement : MonoBehaviour
- {
- public float rotationPeriod = 0.3f;
- public float sideLength = 1f;
- public float jumpTimer = 1.5f;
- public float InAirMoveSpeed = 5.5f;
- bool isRotate = false;
- public bool ThirdPerson = true;
- float directionX = 0;
- float directionZ = 0;
- public float x;
- public float y;
- public string platform = "";
- Vector3 startPos;
- float rotationTime = 0;
- float radius;
- Quaternion fromRotation;
- Quaternion toRotation;
- Transform playerFollower;
- float distToGround;
- public bool canJump = true;
- void Start()
- {
- playerFollower = GameObject.FindWithTag("PlayerFollower").transform;
- radius = sideLength * Mathf.Sqrt(2f) / 2f;
- distToGround = GetComponent<Collider>().bounds.extents.y;
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.V))
- {
- ThirdPerson = !ThirdPerson;
- }
- if(Input.GetKey(KeyCode.LeftShift))
- {
- rotationPeriod = 0.3f;
- }
- else if(!Input.GetKey(KeyCode.LeftShift))
- {
- rotationPeriod = 0.4f;
- }
- if(Input.GetKeyDown(KeyCode.Space))
- {
- //Jump
- if(isGrounded() && canJump == true)
- {
- canJump = false;
- Invoke("resetIsJumping", jumpTimer);
- Jump();
- }
- }
- y = -Input.GetAxisRaw("Horizontal");
- x = Input.GetAxisRaw("Vertical");
- if ((x != 0 || y != 0) && !isRotate)
- {
- directionX = y;
- directionZ = x;
- startPos = transform.position;
- fromRotation = transform.rotation;
- transform.Rotate(directionZ * 90, 0, directionX * 90, Space.World);
- toRotation = transform.rotation;
- transform.rotation = fromRotation;
- rotationTime = 0;
- if(isGrounded() == true)
- {
- isRotate = true;
- }
- }
- }
- void FixedUpdate()
- {
- if (ThirdPerson && isRotate )
- {
- rotationTime += Time.fixedDeltaTime;
- float ratio = Mathf.Lerp(0, 1, rotationTime / rotationPeriod);
- float thetaRad = Mathf.Lerp(0, Mathf.PI / 2f, ratio);
- float distanceX = -directionX * radius * (Mathf.Cos(45f * Mathf.Deg2Rad) - Mathf.Cos(45f * Mathf.Deg2Rad + thetaRad));
- float distanceY = radius * (Mathf.Sin(45f * Mathf.Deg2Rad + thetaRad) - Mathf.Sin(45f * Mathf.Deg2Rad));
- float distanceZ = directionZ * radius * (Mathf.Cos(45f * Mathf.Deg2Rad) - Mathf.Cos(45f * Mathf.Deg2Rad + thetaRad));
- transform.position = new Vector3(startPos.x + distanceX, startPos.y + distanceY, startPos.z + distanceZ);
- transform.rotation = Quaternion.Lerp(fromRotation, toRotation, ratio);
- if (ratio == 1)
- {
- isRotate = false;
- directionX = 0;
- directionZ = 0;
- rotationTime = 0;
- }
- }
- if(!isGrounded())
- {
- transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * InAirMoveSpeed, 0, Input.GetAxis("Vertical") * Time.deltaTime * InAirMoveSpeed, Space.World);
- }
- }
- public bool isGrounded()
- {
- return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
- }
- private void resetIsJumping()
- {
- canJump = true;
- }
- private void Jump()
- {
- GetComponent<Rigidbody>().AddForce(new Vector3(0, 500, 0));
- }
- private void OnCollisionStay(Collision collision)
- {
- if(collision.transform.tag == "Platform")
- {
- //On platform
- platform = collision.gameObject.name;
- }
- }
- private void OnCollisionExit(Collision collision)
- {
- if(collision.transform.tag == "Platform")
- {
- //No longer on platform
- platform = "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement