|
|
- using UnityEngine;
- using System.Collections;
-
- public class thirdPersonController : MonoBehaviour {
-
- public string CAMERA_INPUT_X;
- public string CAMERA_INPUT_Y;
- public string MOVEMENT_INPUT_X;
- public string MOVEMENT_INPUT_Y;
- public string JUMP_INPUT;
-
- public Animator animator;
-
- public Camera camera;
- public GameObject cameraCentre;
-
- public float cameraSpeedX = 250.0f;
- public float cameraSpeedY = 120.0f;
- public float cameraDistance = 10.0f;
- public float maxY = 80;
- public float minY = -20;
- public bool active = true;
-
- public float movementSpeed;
- public bool movementLock;
- public float jumpHeight;
- public float grip;
- public float maxVelocity;
- public float maxAirVelocity;
- private float curMaxVelocity;
- public bool slowed;
- private float curSpeed;
-
- public float cameraX;
- public float cameraY;
- private float movementX;
- private float movementY;
-
- private bool grounded;
-
- private Rigidbody rigidbody;
-
- public Collider curCollider;
-
-
-
-
-
- // Use this for initialization
- void Start () {
-
- rigidbody = GetComponent<Rigidbody> ();
-
- cameraX = camera.transform.eulerAngles.x;
- cameraY = camera.transform.eulerAngles.y;
-
- }
-
- void FixedUpdate(){
-
- RaycastHit hit;
-
- Ray ray = new Ray(transform.position, -Vector3.up);
-
-
- if (Physics.Raycast (ray, out hit, 1.3f)) {
- grounded = true;
- curMaxVelocity = maxVelocity;
- if (hit.collider.tag == "moveable")
- matchVelocity(hit.collider);
-
- } else {
- grounded = false;
- curMaxVelocity = maxAirVelocity;
- animator.SetBool ("Jump", false);
- }
-
- if (slowed)
- curSpeed = 700.0f;
- else
- curSpeed = movementSpeed;
-
-
-
- if (active) {
-
- applyMovement ();
-
- Vector3 checkVelocity = rigidbody.velocity;
- //Debug.Log ("VelMag: " + checkVelocity.magnitude);
- if (checkVelocity.magnitude > maxVelocity){
- float vertVelo = checkVelocity.y;
- checkVelocity = Vector3.ClampMagnitude (checkVelocity, curMaxVelocity);
- checkVelocity.y = vertVelo;
- //Debug.Log (checkVelocity);
- rigidbody.velocity = checkVelocity;
- }
-
- transform.rotation = Quaternion.Euler (new Vector3 (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f));
-
- applyGrip();
-
- applyJump (jumpHeight);
- }
-
- animator.SetBool ("Grounded", grounded);
- animator.SetFloat("Run speed",new Vector3 (rigidbody.velocity.x, 0.0f,rigidbody.velocity.z ).magnitude * Mathf.Sign(movementY));
-
- rigidbody.AddForce (new Vector3 (0.0f, -9.81f,0.0f));
-
- curCollider = null;
- }
-
-
- //}
-
- // Update is called once per frame
- void LateUpdate () {
- if (active) {
- cameraX += Input.GetAxis (CAMERA_INPUT_X) * cameraSpeedX * 0.02f;
- cameraY += Input.GetAxis (CAMERA_INPUT_Y) * cameraSpeedY * 0.02f;
- }
-
- cameraY = ClampAngle (cameraY, minY, maxY);
-
- Quaternion cameraRotation = Quaternion.Euler (cameraY, cameraX, 0.0f);
- Vector3 cameraPosition = cameraRotation * new Vector3(0.0f, 0.0f, -cameraDistance) + cameraCentre.transform.position;
-
- camera.transform.rotation = cameraRotation;
- camera.transform.position = cameraPosition;
-
-
- }
-
- private void applyMovement(){
- movementX = Input.GetAxis (MOVEMENT_INPUT_X);
- movementY = Input.GetAxis (MOVEMENT_INPUT_Y);
-
- if (movementLock) {
- movementX = 0;
- if (movementY < 0 && this.name == "Player2")
- movementY = 0;
- if (movementY > 0 && this.name == "Player1")
- movementY = 0;
- }
-
- Vector3 velocity = new Vector3 (movementX, 0.0f, movementY) * curSpeed * Time.deltaTime;
- //Debug.Log (velocity);
- velocity = Quaternion.Euler (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f) * velocity;
-
- rigidbody.AddForce (velocity);
- }
-
- private void applyJump(float power){
- if (grounded) {
- if (Input.GetButtonDown (JUMP_INPUT)) {
- rigidbody.AddRelativeForce (0, power, 0);
- animator.SetBool ("Jump", true);
- }
- }
-
- }
-
- private void matchVelocity(Collider target){
-
- Debug.Log ("matching velocity");
-
- //Vector3 velocity = rigidbody.velocity;
- //velocity = target.attachedRigidbody.velocity;
-
- //rigidbody.velocity = velocity;
-
- //transform.parent = target.transform;
- //rigidbody.isKinematic = true;
- }
-
-
- public void applyGrip(){
- float localVelX = transform.InverseTransformDirection (rigidbody.velocity).x;
- float localVelZ = transform.InverseTransformDirection (rigidbody.velocity).z;
- //Debug.Log ("velocity.y: " + localVelZ);
-
- if (grounded && Mathf.Abs(movementX) < 0.8f ) {
- rigidbody.AddForce(transform.right * -localVelX * grip);
-
- if (Mathf.Abs(movementY) < 0.5f && Mathf.Abs(localVelZ )> 1.0f){
- rigidbody.AddForce(-transform.forward * grip * localVelZ );
- }
-
- }
-
-
-
-
- }
-
- void OnCollisionStay(Collision col) {
- if (col.transform.tag == "moveable")
- curCollider = col.collider;
- }
-
- private static float ClampAngle (float angle, float min, float max) {
- if (angle < -360)
- angle += 360;
- if (angle > 360)
- angle -= 360;
- return Mathf.Clamp (angle, min, max);
- }
-
-
- }
|