|
|
- 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 Camera camera;
- public GameObject cameraCentre;
-
- public float cameraSpeedX = 250.0f;
- public float cameraSpeedY = 120.0f;
- public float cameraDistance = 10.0f;
- public bool active = true;
-
- public float movementSpeed;
- public float jumpHeight;
- public float grip;
-
- public float cameraX;
- public float cameraY;
- private float movementX;
- private float movementY;
-
- private bool grounded;
-
- private Rigidbody rigidbody;
-
-
-
-
-
- // 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;
- if (hit.collider.tag == "moveable")
- matchVelocity(hit.collider);
-
- } else {
- grounded = false;
- }
- if (active) {
-
- applyMovement ();
-
- transform.rotation = Quaternion.Euler (new Vector3 (0.0f, camera.transform.rotation.eulerAngles.y, 0.0f));
-
- applyGrip();
-
- applyJump (jumpHeight);
- }
-
-
- }
-
-
- //}
-
- // 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;
- }
- 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);
-
- Vector3 velocity = new Vector3 (movementX, 0.0f, movementY) * movementSpeed * 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);
- }
- }
-
- }
-
- 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;
- }
-
-
- private 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 (movementY < 0.5f && localVelZ > 1.0f){
- rigidbody.AddForce(-transform.forward * grip * localVelZ );
- }
-
- }
-
-
-
-
- }
-
-
- }
|