Advertisement
Guest User

Untitled

a guest
Apr 7th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraDrive : MonoBehaviour
  5. {
  6.     public GameObject
  7.         targetObject;
  8.    
  9.     public Transform
  10.         camPivot,
  11.         camTarget,
  12.         camRoot,
  13.        
  14.         relcamdirDebug;
  15.        
  16.     float
  17.         rot = 0;
  18. //----------------------------------------------------------------------------------------------------------
  19.     void Start()
  20.     {
  21.         this.transform.position = targetObject.transform.position;
  22.         this.transform.rotation = targetObject.transform.rotation;
  23.     }
  24.    
  25.     void FixedUpdate()
  26.     {      
  27.     //the pivot system
  28.         camRoot.position    = targetObject.transform.position;
  29.        
  30.     //input on pivot orientation
  31.         rot = 0;
  32.         float mouse_x       = Input.GetAxisRaw( "camera_analog_X" );                //
  33.         rot                 = rot + ( 0.1f * Time.deltaTime * mouse_x );            //
  34.         wrapAngle( rot );                                                           //
  35.        
  36.     //when the target object rotate, it rotate too, this should not happen
  37.  
  38.                
  39.         UpdateOrientation(this.transform.forward,targetObject.transform.up);
  40.  
  41.         camRoot.transform.RotateAround(camRoot.transform.up,rot);
  42.        
  43.        
  44.     //this camera
  45.         this.transform.position = camPivot.position;                            //set the camera to the pivot
  46.         Quaternion target = Quaternion.LookRotation(targetObject.transform.position - this.transform.position, targetObject.transform.up);
  47.        
  48.         this.transform.rotation = Quaternion.Slerp(this.transform.rotation, target, 0.1f);
  49.        
  50.     //debug the relcam dir
  51.        RelativeCamDirection();
  52.  
  53.     //this.transform.LookAt( camTarget.position );                          //
  54.     }
  55. //----------------------------------------------------------------------------------------------------------
  56.     public float wrapAngle ( float Degree )
  57.     {
  58.         while (Degree < 0.0f)
  59.         {
  60.             Degree = Degree + 360.0f;
  61.         }
  62.         while (Degree >= 360.0f)
  63.         {
  64.             Degree = Degree - 360.0f;
  65.         }
  66.         return Degree;
  67.     }
  68.    
  69.     private void UpdateOrientation( Vector3 forward_vector, Vector3 ground_normal )
  70.     {
  71.         Vector3
  72.             projected_forward_to_normal_surface = forward_vector - ( Vector3.Dot( forward_vector, ground_normal ) ) * ground_normal;
  73.        
  74.         camRoot.transform.rotation = Quaternion.LookRotation( projected_forward_to_normal_surface, ground_normal );
  75.     }
  76.    
  77.     float GetOffsetAngle( float targetAngle, float DestAngle )
  78.     {
  79.             return ((targetAngle - DestAngle + 180)% 360)  - 180;
  80.     }
  81. //----------------------------------------------------------------------------------------------------------
  82.     void OnDrawGizmos()
  83.     {
  84.         Gizmos.DrawCube(
  85.             camPivot.transform.position,
  86.         new Vector3(1,1,1)
  87.             );
  88.        
  89.         Gizmos.DrawCube(
  90.             camTarget.transform.position,
  91.         new Vector3(1,5,1)
  92.             );
  93.        
  94.         Gizmos.DrawCube(
  95.             camRoot.transform.position,
  96.         new Vector3(1,1,1)
  97.             );
  98.     }
  99.    
  100.     void OnGUI()
  101.     {
  102.         GUI.Label(new Rect(0,80,1000,20*10), "targetObject.transform.up : " + targetObject.transform.up.ToString());
  103.         GUI.Label(new Rect(0,100,1000,20*10), "target euler : " + targetObject.transform.eulerAngles.y.ToString());
  104.         GUI.Label(new Rect(0,100,1000,20*10), "rot : " + rot.ToString());
  105.  
  106.     }
  107. //----------------------------------------------------------------------------------------------------------
  108.     void RelativeCamDirection()
  109.     {
  110.                
  111.         float
  112.             input_vertical_movement     = Input.GetAxisRaw( "Vertical" ),
  113.             input_horizontal_movement   = Input.GetAxisRaw( "Horizontal" );
  114.        
  115.         Vector3
  116.             relative_forward    = Vector3.forward,
  117.             relative_right      = Vector3.right,
  118.            
  119.             relative_direction  =
  120.                 ( relative_forward  * input_vertical_movement )
  121.                 +
  122.                 ( relative_right    * input_horizontal_movement )
  123.                 ;
  124.        
  125.         MovementController MC = targetObject.GetComponent<MovementController>();
  126.        
  127.         Vector3 direction = relative_direction.normalized * MC.acceleration * Time.fixedDeltaTime;
  128.        
  129.         Vector3 abs_direction = this.transform.TransformDirection( direction );
  130.         MC.motion = abs_direction;
  131.        
  132.         //MC.transform.Rotate(Vector3.up, input_horizontal_movement * 10f * Time.fixedDeltaTime);
  133.  
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement