Advertisement
Guest User

Untitled

a guest
Apr 7th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MovementController : MonoBehaviour
  5. {
  6.     public float
  7.         deadZoneValue = 0.1f,
  8.         angle,
  9.         acceleration  = 50.0f;
  10.     public Vector3
  11.         motion ;
  12.     public Vector3
  13.         ground_direction ;
  14.  
  15. //-------------------------------------------------------------------------------------------- 
  16.     void OnGUI()
  17.     {
  18.         GUILayout.Label( "transform.rotation : " + transform.rotation );
  19.         GUILayout.Label( "transform.position : " + transform.position );
  20.         GUILayout.Label( "angle : " + angle );
  21.         GUILayout.Label( "ground_direction : " + ground_direction );
  22.         GUILayout.Label( "motion : " + ground_direction );
  23.     }
  24.    
  25.     void FixedUpdate ()
  26.     {
  27.        
  28.         Ray
  29.             ground_check_ray = new Ray( gameObject.transform.position, -gameObject.transform.up );
  30.         RaycastHit
  31.             raycast_result;
  32.         Rigidbody
  33.             rigid_body = gameObject.rigidbody;
  34.        
  35.         if ( Physics.Raycast( ground_check_ray, out raycast_result ) )
  36.         {
  37.             Vector3
  38.                 next_position;
  39.            
  40.             //UpdateOrientation( gameObject.transform.forward, raycast_result.normal );
  41.             UpdateOrientation( gameObject.transform.forward, raycast_result.normal );  
  42.             next_position = GetNextPosition( raycast_result.point );
  43.             rigid_body.MovePosition( next_position );
  44.             ground_direction = raycast_result.normal;
  45.         }
  46.     }
  47. //-------------------------------------------------------------------------------------------- 
  48.    
  49.    
  50.     private void UpdateOrientation( Vector3 forward_vector, Vector3 ground_normal )
  51.     {
  52.         Vector3
  53.             projected_motion_to_normal_surface = motion - ( Vector3.Dot( motion, ground_normal ) ) * ground_normal;
  54.        
  55.         Quaternion target;
  56.         if (motion.sqrMagnitude > 0)
  57.         {
  58.             target = Quaternion.LookRotation( projected_motion_to_normal_surface, ground_direction );
  59.         }
  60.         else
  61.         {
  62.             target = Quaternion.LookRotation( transform.forward, ground_direction );
  63.         }
  64.         transform.rotation  = Quaternion.Slerp( transform.rotation, target, 0.1f );
  65.         //transform.rotation = Quaternion.LookRotation( projected_forward_to_normal_surface, ground_normal );
  66.     }
  67.    
  68.     private Vector3 GetNextPosition( Vector3 current_ground_position )
  69.     {
  70.         Vector3
  71.             next_position;
  72.  
  73. //      //--------------------------------------------------------------------
  74. //      angle = 0;
  75. //      Vector3 dir = this.transform.InverseTransformDirection(motion);
  76. //      angle = Vector3.Angle(Vector3.forward, dir);// * 1f * Time.fixedDeltaTime;
  77. //     
  78. //      if(angle > 0) this.transform.Rotate(0,angle,0);
  79. //      //--------------------------------------------------------------------
  80.        
  81.        
  82.         next_position =
  83.             current_ground_position +
  84.             gameObject.transform.up * 0.5f
  85.                 + motion
  86.                 ;
  87.        
  88.         return next_position;
  89.     }
  90.    
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement